Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

Re: sort

  • To: mathgroup at
  • Subject: [mg29997] Re: [mg29991] sort
  • From: BobHanlon at
  • Date: Sat, 21 Jul 2001 00:49:04 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

In a message dated 2001/7/20 3:36:01 AM, nails at writes:

>Does anybody now how to sort a matrix first by one column and then by
>another column (giving priority to the first column and sorting by
>second column only when there are ties in the first column).

sortByCol[l_List, n_Integer] := 
    Sort[l, #1[[n]] <= #2[[n]]&];

mat = Table[Random[Integer, {1, 4}], {10}, {10}];

sortByCol[mat, 3]

To sort by column 3 with column 4 breaking ties

sortByCol[sortByCol[mat, 4], 3]

If more than two columns are to be used 

Fold[sortByCol[#1, #2]&, mat, {5, 4, 3}]

Bob Hanlon
Chantilly, VA  USA

  • Prev by Date: Re: Integrate[Sqrt[Tan[x]], {x, 0, 1}] -- approx ??
  • Next by Date: Re: Integrate[Sqrt[Tan[x]], x]
  • Previous by thread: Re: sort
  • Next by thread: Re: sort