2

Currently I'm working on a project where I'm required to sort a total of 6 arrays.

I've managed to sort the arrays individually using a quicksort, however, I'm just wondering if there is a way to sort 1 array and reflect that on the order of elements in the other arrays. For example, if I sort my date array to an ascending order, I want the other arrays to still match up with the dates in respect to the new order.

If possible, could this still be done through a quick sort?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
PTR01
  • 83
  • 2
  • 6
  • 4
    There probably is a way, but it seems a much better approach to your problem is to have a single array of objects, and sort the 1 array by their date – Kevin DiTraglia Apr 12 '15 at 16:58
  • There is `Array.Sort` which lets you sort an array by a keys array. – SimpleVar Apr 12 '15 at 17:01
  • It's easier to do if you pack all the data of the 6 arrays in a single structure (its own class or a Tuple) and give your QuickSort function a lambda or `Func` to sort by, then split the result after sorting. – Mephy Apr 12 '15 at 17:01

3 Answers3

3

I think the more appropriate option will be to create a new class with all different 6 kind of properties

public class myClass
{
    public DateTime date{get;set;}
    public string name{get;set;}
    //....
}

Then create a single array/list of that class.

public List<myClass> arrData;

Now you can sort that array based on any of your desired property and it will keep the order as per your requirements

arrData.OrderBy(x => x.name)

You can replace x.name with any of your myClass property.

This approach makes your code clean and easy to manage as well.

Haseeb Asif
  • 1,766
  • 2
  • 23
  • 41
2

It looks like LINQ uses the quick sort algorithm for the OrderBy method (see previous StackOverflow question).
Something like this should take care of it for you:

DateTime[] datesOfBirth = new DateTime[] { new DateTime(1955, 10, 28), new DateTime(1955, 2, 24) };
String[] firstNames = new String[] { "William", "Steve" };
String[] lastNames = new String[] { "Gates", "Jobs" };

var people = 
    datesOfBirth
    .Select((_, i) => new 
        { 
            DateOfBirth = datesOfBirth[i],
            FirstName = firstNames[i],
            LastName = lastNames[i]
        })
    .OrderBy(x => x.DateOfBirth)
    .ToArray();
Community
  • 1
  • 1
Jason Boyd
  • 6,839
  • 4
  • 29
  • 47
1

Likely you have a setup something like this:

DateTime [] dates;
string [] names;
int [] ids;
//...etc

Consider instead to condense your data into a single object, then have a single array of that object:

public class MyObject 
{
    DateTime date { get; set; }
    string name { get; set; }
    int id { get; set; }
}

Now you will only have 1 array:

MyObject [] objects;

And you can sort the whole collection of objects by their date:

objects.Sort((a, b) => a.date.CompareTo(b.date));

Also consider using Lists rather than straight arrays, as the use case for using vanilla arrays in c# is very small:

List<MyObject> objects;
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • @HaseebAsif years of programming will cause that =P – Kevin DiTraglia Apr 12 '15 at 17:19
  • sorry for the late reply, i've been busy >.< ok this looks promising, however will this still work if I applied this to a custom quick sort method instead of the inbuilt .Sort? For this project i'm not allowed to use in-built sorting mechanisms since I will need to calculate the efficiency of my program...that and regulations >.> – PTR01 Apr 14 '15 at 12:18
  • @PTR01 I don't see why not, as long as your custom quick sort knows to pull the date value off the object – Kevin DiTraglia Apr 14 '15 at 16:38