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?

3
  • 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 Commented Apr 12, 2015 at 16:58
  • There is Array.Sort which lets you sort an array by a keys array. Commented Apr 12, 2015 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. Commented Apr 12, 2015 at 17:01

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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();

Comments

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;

3 Comments

@HaseebAsif years of programming will cause that =P
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 I don't see why not, as long as your custom quick sort knows to pull the date value off the object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.