Skip to main content
Added comparison function example
Source Link
Felix Kasza
  • 869
  • 4
  • 19

Not a bug. The docs: "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth‐first manner."

You want SortBy[list,N], I think. For more complex cases, use Ordering[] to get a list of indexes and use that to reorder the original list:

Ordering@N@list list[[%]] 

Perhaps you should consider the option of handing Sort[] your own ordering predicate. Just use any pure function whatsoever which works on #1 and #2 and returns True if #1 comes before #2 in your desired sort order, or False otherwise:

peopleAndAges={{"Felix",50},{"Max",19},{"Sophie",22}}; CompareByName[{n1_String,_},{n2_String}]:=(ToLowerCase@n1 <= ToLowerCase@n2) CompareByAge[{_,a1_},{_,a2_}]:=(a1 >= a2) Sort[peopleAndAges,CompareByAge] 

Not a bug. The docs: "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth‐first manner."

You want SortBy[list,N], I think. For more complex cases, use Ordering[] to get a list of indexes and use that to reorder the original list:

Ordering@N@list list[[%]] 

Not a bug. The docs: "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth‐first manner."

You want SortBy[list,N], I think. For more complex cases, use Ordering[] to get a list of indexes and use that to reorder the original list:

Ordering@N@list list[[%]] 

Perhaps you should consider the option of handing Sort[] your own ordering predicate. Just use any pure function whatsoever which works on #1 and #2 and returns True if #1 comes before #2 in your desired sort order, or False otherwise:

peopleAndAges={{"Felix",50},{"Max",19},{"Sophie",22}}; CompareByName[{n1_String,_},{n2_String}]:=(ToLowerCase@n1 <= ToLowerCase@n2) CompareByAge[{_,a1_},{_,a2_}]:=(a1 >= a2) Sort[peopleAndAges,CompareByAge] 
Source Link
Felix Kasza
  • 869
  • 4
  • 19

Not a bug. The docs: "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth‐first manner."

You want SortBy[list,N], I think. For more complex cases, use Ordering[] to get a list of indexes and use that to reorder the original list:

Ordering@N@list list[[%]]