0

I have a byte array, for example byte[] = new byte[3] { 97, 98, 99, 99, 96 } In real situation byte array is much longer.

How can I get all rotations of this array and sort it? And also I need to keep original index in sorted list.

Rotations should be like:

{ 97, 98, 99, 99, 96 },
{ 98, 99, 99, 96, 97 },
{ 99, 99, 96, 97, 98 },
{ 99, 96, 97, 98, 99 },
{ 96, 97, 98, 99, 99 }

Then I need to sort it to get:

{ 96, 97, 98, 99, 99 },
{ 97, 98, 99, 99, 96 },  // <- index in rotated list/array
{ 98, 99, 99, 96, 97 },
{ 99, 96, 97, 98, 99 },
{ 99, 99, 96, 97, 98 }

I was using not efficient way to convert byte[] to string, then create array of strings, each array element keep it's rotations and after that sort array of strings. I was also using built-in function "sort" so I couldn't catch my index. Maybe it's possible to make this with LINQ or something similar?

2 Answers 2

1

You can extend the array and do your sorting there. You will need to create your own sorting mechanism:

public static int[] SortAndReturnIndexes(this byte[])
{
    var indexArray = new int[];
    // Your sort logic goes here
    return indexArray;
}
0

Depending on what you want to do with it after you did these operations, physically producing the results may not be the best thing to do. Take for example the following class:

public class Rotation[T] {

  private int init;
  private T[] src;

  public Rotation(T[] src, init){
    this.init = init;
    this.src = src;
  }

  public T this[int i] { //indexer
    get {
      int realindex = (i + init) % src.Length; //rotate the index
      return src[realindex];
    }
  }
}

you could use it as

byte[] src = new byte[5] { 97, 98, 99, 99, 96 }

//rotate by 2

Rotation[byte] rotation = new Rotation(src, 2);

rotation[0] == 99

To sort it, it's best to create a custom IComparer, which you can feed to sort methods: https://msdn.microsoft.com/en-us/library/8ehhxeaf%28v=vs.110%29.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.