
April 5th, 2013, 07:15 PM
#1
arbitrary sort algorithm
Hi, does anyone know of a C++ sort algorerhythm or library that can take input of a bunch of rows of data and then sort rows by an arbitrary defined order of one of the columns
ie sort rows by value of the first column in this order (boba bobc bobe bobx) etc?
Last edited by Jarwulf; April 5th, 2013 at 08:31 PM.

April 7th, 2013, 02:08 AM
#2
Re: arbitrary sort algorithm
Originally Posted by Jarwulf
a C++ sort algorerhythm or library that can take input of a bunch of rows of data and then sort rows by an arbitrary defined order of one of the columns
You can quite easily sort a 2Darray using the standard C++ sort algorithm. If the 2Darray has a normal C++ memory layout it's most efficient to first create an array holding pointers to the rows. Then this row pointer array is sorted using a sort criterion which decides the pairwise order between row pointers based on row contents. Finally the original 2Darray is copied to a new 2D array according to the sorted row order available in the row pointer array. Alternatively you do this inplace in the original using some swapping scheme.
Note that if the 2Darray often is handled rowwise it may be convenient not to use the standard C++ memory layout, but to switch to a 1Darrayofpointerspointingto1Darraysofelements data structure. This very much simplifies the sorting process described above because the row pointer array now already exists as part of the 2Darray.
Last edited by nuzzle; April 7th, 2013 at 03:44 AM.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
