0

Is there an easy way to sort a list starting at a particular value?

A B C D E F

So we can start from a passed in value say 'C'

C D E F A B

I want optimise a time slot list and it will be best to sort the order from a starting time so it doesn't need to iterate all times it doesn't need to.

  • Please elaborate. The example you have given is already sorted and it looks more like reordering of elements?! – Hitesh P Sep 28 '16 at 19:46
  • Sorry about that. I can order using anonymous function ok as most do for simple sorting. so list becomes sorted already but would like a custom starting point for the order to start. – New_Boy_2020 Sep 28 '16 at 19:49
  • Implement your own mergesort algorithm and send the range you want to sort. – raven Sep 28 '16 at 19:51
  • Further to this. I'm defining a list of time slots , 08:00, 10:00, 17:00, 19:00, 22:00 etc. I'm checking every minute of a time frame to find which slot each minute fits. A lot of minutes through the day so just trying to optimise a little of there are a lot of timeslots. It's not too bad for 1-3. My current code stops checking when each minute is assigned. – New_Boy_2020 Sep 28 '16 at 19:51
  • You should just edit your question to add the details you are explaining in the comments to the question itself to make it easier for people to answer. – Jacobr365 Sep 28 '16 at 20:05
  • I was trying to keep it as simple as possible and understandable. I've done a bad job. Currently on the phone so will amend when I'm back at a PC. – New_Boy_2020 Sep 28 '16 at 20:09

2 Answers2

0

You could use:

ArrayList.Sort(int index, int count, IEqualityComparer comparer)

This method sorts the elements in a range of elements in ArrayList using the specified comparer.

Parameters

index

Type: System.Int32 The zero-based starting index of the range to sort.

count

Type: System.Int32

The length of the range to sort.

comparer

Type: System.Collections.IComparer The IComparer implementation to use when comparing elements.

-or-

A null reference (Nothing in Visual Basic) to use the IComparable implementation of each element.

There is an example right here explaining how to use it.

ArrayList.Sort Method

If you don't know how to implement the Equality comparer you can check this post too

Update:

From your array: A B C D E F

Let's suppose that it is in this object and it is instantiated:

List<string> array;

So you can do this:

public List<string> foo(List<string> array, int i) { 
  array.Sort(i, array.size() - i, null);
  List<string> aux = array.Skip(i).Take(array.Size() - i );
  aux.AddRange(array.Take(i));
  return aux;
}

and you call it like this:

List<string myNewArray = foo(array, 2);
Community
  • 1
  • 1
raven
  • 2,381
  • 2
  • 20
  • 44
  • But does that just leave first part of the list unsorted anywhere above the starting index? – New_Boy_2020 Sep 28 '16 at 20:04
  • yes, it will sort starting from index, n elements. Where n is the count parameter. – raven Sep 28 '16 at 20:05
  • But I want the closest match to be end up at index 0 then everything else in order from there then cycle round. B, C, F, A, D,E to become 'C', D, E, F, A, B – New_Boy_2020 Sep 28 '16 at 20:15
  • 1
    Split into two parts, sort both separately and append the second part to the first. – Hitesh P Sep 28 '16 at 20:16
  • That splitting did cross my mind as an option. I thought it was a little manual and was hoping for some clever .net function I hadn't come across. – New_Boy_2020 Sep 28 '16 at 20:20
  • Thanks Roberto Think that's pretty much done it, I made a few amends.# – New_Boy_2020 Sep 28 '16 at 21:53
  • Thanks Roberto Think that's pretty much done it, I made a few amends. Public Function foo(_array As List(Of String), i As String) As List(Of String) _array.Sort() Dim foundIndex As Integer = _array.FindIndex(Function(x) x > "C") Dim aux As List(Of String) = _array.Skip(foundIndex).Take(_array.Count - foundIndex).ToList aux.AddRange(_array.Take(foundIndex)) Return aux End Function – New_Boy_2020 Sep 28 '16 at 22:01
0
    static void Main(string[] args)
    {
        char[] z = new char[] { 'B', 'C', 'F', 'A', 'D', 'E' };
        Array.Sort(z);

        char[] x,y;
        Split<char>(z, 2, out x, out y);

        char[] combined = y.Concat(x).ToArray ();
    }
    public static void Split<T>(T[] array, int index, out T[] first, out T[] second)
    {
        first = array.Take(index).ToArray();
        second = array.Skip(index).ToArray();
    }
Hitesh P
  • 406
  • 2
  • 5
  • 12