473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Maintain initial sort order with 2d array sort

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
Nov 16 '05 #1
3 2945
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.

Nov 16 '05 #2
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

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:BB**************@cpmsftngxa10.phx.gbl...
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.

Nov 16 '05 #3
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.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: TekWiz | last post by:
I've got a system that automatically generates a form. I have it set up so that the backend will return to the inital form page with an error object in sessions data (assuming the backend detected...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
4
by: Brett | last post by:
I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array. ie. @l1={3,1,2};...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
22
by: AB | last post by:
Hello All, I'm trying to replicate a general purpose sort function (think qsort) void sort(void *arr, const int num, size_t size, int (*cmp)(void *a, void *b)) { int i = 0 ; int j = 0 ;
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: Joseph Geretz | last post by:
I've been looking at two approaches for the maintenance of Session state for a Web Service application. One approach uses the old familiar Session object which I've used in the past for Web...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.