0

I'm trying to sort a list of intervals based on start time (which is the first element).

public static int[][] SortIntervals(int[][] intervals)
{
    intervals.Sort(Comparer<int[]>.Create((a, b) => a[0].CompareTo(b[0]))); // error
    return intervals;
}

Error -CS1503 Argument 1: cannot convert from 'System.Collections.Generic.Comparer<int[]>' to 'System.Array'

I can sort a list of intervals just fine, maybe I'm missing the correct syntax for it.

public static List<int[]> SortIntervals(List<int[]> intervals)
{
    intervals.Sort(Comparer<int[]>.Create((a, b) => a[0].CompareTo(b[0])));
    return intervals;
}
1
  • Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0])); Commented Jan 13, 2022 at 4:55

1 Answer 1

3

You are right, in case of array you should use different syntax Array.Sort:

public static int[][] SortIntervals(int[][] intervals)
{
    if (null == intervals)
        throw new ArgumentNullException(nameof(intervals));

    Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0]));    

    return intervals;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great, thank you!

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.