3

I have an array:

int[] months = new int[4] {1, 4, 7, 10};

I would like to sort the array starting by the given value and sort the rest of the array in the original order.

Let's say I want to start sorting the array by a value of 7. The sorted array would be then in order of:

7, 10, 1, 4

Or starting with a value 4 the sorted array would be an order of
4, 7, 10, 1

3 Answers 3

3

How about:

var orderedMonths = months.Where(x => x >= 7)
                          .OrderBy(x => x)
                          .Concat(months.Where(x => x < 7));

Note that this will mean that the elements of the "rest of the array" will be in order of appearance rather than increasing numeric order. If you meant the latter (i.e. sort both 'segments' numerically) , I would do:

var orderedMonths = months.OrderBy(x => x < 7) // false comes before true
                          .ThenBy(x => x);

On the other hand, if you want to sort both segments by order of appearance, I would do:

var orderedMonths = months.GroupBy(x => x < 7)
                          .OrderBy(group => group)
                          .SelectMany(x => x);

(or)

var orderedMonths = months.Where(x => x >= 7)
                          .Concat(months.Where(x => x < 7));
Sign up to request clarification or add additional context in comments.

7 Comments

You can use .Where and .OrderBy with lambda values on an array?
@TravisJ: Sure, an int[] is an IEnumerable<int>. The compiler will then use the LINQ to Objects extension-methods on IEnumerable<T> in the Enumerable class.
@Ani: sems to me that the algorithm is not correct, cause if you look on specification provided by OP, you will see that the sort is not done by value provided, but by index of that value in array. In other words selecting something by >=7 is not correct.
@Tigran: It's really not clear either way because the provided array is already sorted. I understand what you mean though. Let's wait for clarification from the OP. :)
@Ani Oh cool, I didn't know that :) I thought those operators were only available for modeled objects, but it makes sense that they would apply to all objects which were Enumerable.
|
0

Assuming this is your sorted int array you could

int[] months = new int[4] { 1, 4, 7, 10 };
int value = 10; 
int[] chk1 = new int[4];
chk1 = months.SkipWhile(a => a != value).
                Concat(months.TakeWhile(a => a != value)).ToArray();

This should get you the required order

2 Comments

Going to be hard to do this if the array needs to be dynamic though, as far as making chk1 goes.
Ah, I am not near a compiler and wasn't sure how strict the restrictions on initiating arrays were.
0

Can you use a list?

int NumberToBeFound = 7;
int IndexOfNumber = -1;
for(int i=0;i<months.count;i++){
  if(months[i] == NumberToBeFound){
    IndexOfNumber = i;
    break;
  }
}
List<int> Sorted = new List<int>();
for(int i = IndexOfNumber; i < months.count;i++){
  Sorted.Add(months[i]);
}
for(int i = 0; i < IndexOfNumber; i++){
  Sorted.Add(months[i]);
}
months = Sorted.ToArray();

Comments

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.