-2

I thought I'd find everything I need on sorting online, but this one eludes me. I have a single instance of a class. The class has two arrays. I have to sort the combined two elements of the class based on one of the two arrays. It's easy to sort a single array - but how do I sort the combination of the two? I thought it would involve IComparable, but that seems to be for multiple instances of the class, not just one.

The class (I want to sort on data, descending):

public class RGraphData
{
    public int[] data { get; set; }
    public string[] labels { get; set; }
}

Note that the number of items in both data and labels are identical.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
JohnC.IT
  • 33
  • 5
  • 1
    Is each label[i] associated with data[i]? If this is an implementation you have control over, then storing the data this way is not ideal, especially if sorting is a common operation. Perhaps you want to store key/value pairs in a list, or an IDictionary? Otherwise, you need to associate the label items with the data items before performing a sort. – nlawalker Feb 05 '18 at 22:31
  • 1
    *"Note that the number of items in both data and labels are identical"* Given that these are both public members of the class, this statement does not appear to be at all guaranteed. It sounds like some other, single object should be used to represent these fields – Rufus L Feb 05 '18 at 22:33
  • `I have to sort the combined two elements of the class based on one of the two arrays.` Do you mean sort the one array based on the other? But what does that mean? What relationship is there between the items in the two arrays? How would you know if they had or had not been sorted? – Scott Hannen Feb 05 '18 at 22:35
  • As all the comments indicate there are potentially issues with (your) design. The answers below are good but based on (risky) assumptions. If there is a concrete relationship between each [i] element of the arrays you may consider implementing it as a key value pair i.e. Dictionary – DaniDev Feb 05 '18 at 22:48
  • As Rufus L points out, you cannot guarantee that both arrays will have the same number of elements based on the current implementation. This would break the perfectly valid answer provided by Bradley Grainger. You would need to add a check before making a call to the Array.Sort provided in the answer. Or, a better overall choice would be to use the appropriate data structure; in this case a Dictionary, as pointed out by nlawalker, because this will enforce the 1:1 relationship throughout, and its elements can be easily organized. – user7396598 Feb 05 '18 at 22:49
  • Thank you, everyone. You've given me several solutions that should work. – JohnC.IT Feb 05 '18 at 23:33

2 Answers2

0

Use the Array.Sort overload that takes two arrays:

Array.Sort(data, labels, new DescendingOrderComparer());

Per MSDN:

Each key in the keys Array has a corresponding item in the items Array. When a key is repositioned during the sorting, the corresponding item in the items Array is similarly repositioned. Therefore, the items Array is sorted according to the arrangement of the corresponding keys in the keys Array.

To sort on data descending, you will need a custom IComparer that compares items in descending order:

public class DescendingOrderComparer : IComparer<int>
{
    public int Compare(int a, int b) => b.CompareTo(a);
}
Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
0

This will associate each data[i] with label[i] as a KeyValuePair and give you an ordered IEnumerable back:

labels.Zip(data, (l, d) => new KeyValuePair<string, int>(l, d))
    .OrderByDescending(p => p.Value);
nlawalker
  • 6,364
  • 6
  • 29
  • 46