Maintain initial sort order with 2d array sort

  • Thread starter Thread starter Bob Dankert
  • Start date Start date
B

Bob Dankert

Is there any way to maintain the sort order of a sort on a 2D array? For
example:

I have the array:

1,a 2,a 3,f 4,a 5,s 6,a 7,z 8,b

and sort it by the second column, and I get:

4,a 6,a 1,a 2,a 8,b 3,f 5,s 7,z

The results I am looking for would be like:

1,a 2,a 4,a 6,a 8,b 3,f 5,s 7,z

I would like the sort to keep in mind the initial sorting, so the final
result has sorting done on two rows (similar to sorting by multiple columns
in a database, or by clicking to sort a windows explorer window first by one
column, then by another).

The code I use for sorting is very basic:

public class comp : IComparer
{
int column;
public comp(int col)
{
column = col;
}

public int Compare(object x, object y)
{
return
objectCompare.Compare(((Array)x).GetValue(column),((Array)y).GetValue(column
));
}
}

I know that my custom listview which allows for sorting by multiple columns
(implemented based on Microsoft's implementation) will work as I am looking
for, so I hope that this is something that I will somewhat-easily be able to
accomplish.

I appreciate any thoughts, help, and suggestions!

Thanks,

Bob Dankert
 
Hi Bob,

Based on my understanding, you want to sort the array with the initial sort
order.

I think this based on your sort algorithm. You should choose some sort
algorithm that will not change the order that 2 items have the same value.

The code you pasted is not the sort algorithm, it is just a compare code.
There are some sort algorithms that will maintain the initial order, such
as Bubble sort etc..

You can follow my suggestion to use Bubble sort to see if it meets your
need. If you have problem about the sort algorithm, I suggestion you find
data structure book, and see the sort chapter.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jeffrey,

I got the code working with a slightly modified merge sort algorithim. I
was hoping I would be able to natively get what I needed from dotnet, but
the code did not turn out to be all that bad in the end to implement my own
sort algorithim.

I appreciate the help guiding me in the right direction with this, thanks!

Bob Dankert
 
Hi Bob,

Thanks for your feedback.

Yes, Merge sort algorithm can also meet your need, because it is steady.

If you need further help, please feel free to post, I will help you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top