MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Sorting with Infinity

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23529] RE: [mg23513] Sorting with Infinity
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 16 May 2000 22:30:00 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com


> From: Jack Goldberg [mailto:jackgold at math.lsa.umich.edu]
To: mathgroup at smc.vnet.net
> Hi group;
> 
> I construct lists which may look like this
> 
> 	{7,-2,-Infinity,Infinity,1}
> 
> When this list is sorted by using Sort, I get
> 
> 	{-2,1,7,-Infinity,Infinity}
> 
> I would prefer, naturally, 
> 
> 	{-Infinity,-2,1,7,Infinity}
> 
> This is easy to do if one doesn't care about elegance.  Without 
> spelling out the obvious details, here are a few kludges:
> 
> (1) Replace Infinity by 10^15, Sort  then switch back to Infinity.
> (2) Use Append and Prepend after Sort and  deleting infinities.
> (3) RotateRight after Sort and switch signs on infinities
> 
> Anyone got a "cute" solution say by using a sorting function or 
> perhaps some arcane command unknown to mere mortals? 
> 
> Jack
> 

Hi Jack,

Use the second optional argument in Sort to supply a sort criterion.

Sort[{7, -2, -Infinity, Infinity, 1}, 
  #2 > #1 || #1 === -Infinity || #2 === Infinity & ]

{-Infinity, -2, 1, 7, Infinity}

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/ 


  • Prev by Date: Mathematica crashing, short cut keys
  • Next by Date: Re: [TS 3227] ListInterpolation
  • Previous by thread: Re: Sorting with Infinity
  • Next by thread: Re: Sorting with Infinity