Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Mathematica Sorting Anti-Stable?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105262] Mathematica Sorting Anti-Stable?
  • From: beckman <bc.beckman at gmail.com>
  • Date: Wed, 25 Nov 2009 22:59:30 -0500 (EST)

Consider:

In[97]:= sortTestPoints = {{1, 1}, {1, 2}, {2, 1}, {2, 2}}

Out[97]= (1	1
1	2
2	1
2	2

)

To produce a lexical sort, first sort by the second element. Notice it
reverses the order of the first element (first clue that "Sort" might
be antistable)

In[99]:= Sort[sortTestPoints, #1[[2]] < #2[[2]] &]

Out[99]= (2	1
1	1
2	2
1	2

)

Now, sort by the first element

In[100]:= Sort[Sort[sortTestPoints, #1[[2]] < #2[[2]] &], #1[[1]] < #2
[[1]] &]

Out[100]= (1	2
1	1
2	2
2	1

)

But, by itself, without a value for the the optional ordering
function, Sort effects a correct lexical sort.

In[101]:= Sort@sortTestPoints

Out[101]= (1	1
1	2
2	1
2	2

)


  • Prev by Date: Replacing Values Close to One
  • Next by Date: Re: part assigned sequence behavior puzzling
  • Previous by thread: Re: Replacing Values Close to One
  • Next by thread: Re: Mathematica Sorting Anti-Stable?