2

I have a list<> of int arrays created like this

List<uint[]> onMinterm = new List<uint[]>();    

and it has got 1000 members. Every list members has 3 unsigned integers, I add my arrays just like that

uint[] sayi= new uint[3];    
sayi[0] = 34432; sayi[1] = 63533; sayi[2] = 12;    
onMinterm.Add(sayi);

I want to sort my 1000 list (onMinterm) according to each 3rd member (sayi[2]). List will be sorted in decending order. Sample member should be at the end as its 3rd value is very small like 12.

2
  • 1
    onMinTerm.OrderByDescending(a => a[2]);. That said, take a look at meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented May 18, 2015 at 23:40
  • Design-wise you may want your uint[] to actually be a class or a struct. If you regularly have exactly 3 of somethingand you have that many of them, it's sounding like the stuff in the uint may deserve to have its own identity--i.e., these regularly recurring three values may have some related semantic value that deserves to be named and given their own identity an (possibly, via methods) functionality. Commented May 18, 2015 at 23:45

2 Answers 2

3

I want to sort my 1000 list (onMinterm) according to each 3rd member (sayi[2]). List will be sorted descending.

You can do this, if you are ok with getting an IOrderedEnumerable as the result.

var ordered = onMinterm.OrderByDescending(x => x[2]);

If you want to do an in-place sort:

onMinterm.Sort((x1, x2)=> x2[2].CompareTo(x1[2]));
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, thanks. I did not know how to do it this way (in place with Comparison delegate).
1

You can sort a List<> in place.

    class IntArrayComparer : IComparer<int[]>
    {
      public int Compare(T left, T right)
      {
        return right[2].CompareTo(left[2]);
      }
    }

    myList.Sort(new IntArrayComparer());

1 Comment

Note that this will not result in a descending sort order as OP requested in his question.

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.