Re: sort
- To: mathgroup at smc.vnet.net
- Subject: [mg29997] Re: [mg29991] sort
- From: BobHanlon at aol.com
- Date: Sat, 21 Jul 2001 00:49:04 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 2001/7/20 3:36:01 AM, nails at asu.edu 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