0

I'm having problems sorting a list by index using an array as the source of the sort.

Assume there are 5 records set to my class

class Program
{
    static void Main(string[] args)
    {
     int[] myInt {2,1,0,3,4}
     List<Tests> myTests = new List<Tests>; 

     //this part doesn't work

     for (int i = 0; i < 4; i++)
        {
         myInt[i] = myTests[i];
         }
     myTests.ForEach(i => Console.WriteLine("{0} {1}", i.id, i.myString));
    }
}

My Class definition

class Tests
{
     public int iD {get; set;}
     public string myString {get; set;}

     public Tests (int iD, string myString)
    {
       this.iD = iD;
       this.myString = myString
    }
{

What I would like to see come out

     record 2
     record 1
     record 0
     record 3
     record 4

I tried using the sort function for lists but I couldn't find any examples that used an array as the sort criteria so I'm kind of lost. I appreciate any help offered.

4
  • 2
    List<Tests> myTests = new List<Tests>; won't compile. Did you mean new List<Tests>(); or new List<Tests>(myInt);? Commented Dec 6, 2012 at 12:59
  • Can you give us a more complicated example? Your example gives the same output whether you interpret myInt[i] as "the position in the list I want the ith element to end up in" or "the original position of the element which I want to place in position i in the output" (i.e. myInt[0] = 2 - does this say "put record 2 in position 0" or "fill position 2 with record 0"?) Commented Dec 6, 2012 at 13:01
  • It would be a lot easier to answer your questions if you posted code that compiled, or match your question more closely. Commented Dec 6, 2012 at 13:19
  • Sorry I, was having problems with the code compiling in the first place that is why I was here. But, I have realized what I was doing wrong and why it wasn't working thanks to Rawlings comments. However I have realized an easier and probably a more performant way just to simply do a LINQ query on the original records rather than rearranging the objects that represent them. I appreciate everyone's help, Thank you! Commented Dec 7, 2012 at 8:54

4 Answers 4

2

Off the top of my head, something like this should do the trick:

var sortedTests = myInt
    .Select((x,index) => new {test = myTests[x], sortIndex = index})
    .OrderBy(x => x.sortIndex)
    .Select(x => x.test)
    .ToList()

Hmm. In fact, with Linq-to-objects it's somewhat easier:

var sortedTests = myInt
    .Select(x => myTests[x])
    .ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Or possibly .Select((index, x)... depending on which way around OP wants this to work. (Note that, in this example, the OrderBy is unnecessary as the sortIndex values are already in order.)
@Rawling Indeed... I added a considerably simpler version!
Thank you, I actually couldn't get your code to work, but it pointed me into the right direction. I ended up just doing a LINQ Query using the array as the search values. I appreciate your help. Thank you.
0

You should assign the values of myTests to another List

List<Tests> newTests = new List<Tests>();
for (int i = 0; i < 4; i++)
{
    newTests.Add(myTests[myInt[i]]);
}

Comments

0

First:

You can't use myList[i] unless you've created an item of the list in position i.

Second:

You're not calling the Tests constructor to create a new Tests object

Third:

You're assigning to myInt[i] an empty reference myTests[i]

You should have something like:

for (int i = 0; i < 4; i++) {

    myTests.Add(new Tests(i, "foo"))

}

Comments

0

Your provided code is kind of vague, so I cooked up a couple of different scenarios.
1. Creating based on index.
2. Sorting based on index.

Paste it into your IDE and it will work.

 class Program
  {
    static void Main(string[] args)
    {
      int[] myInt = new[] { 2, 1, 0, 3, 4 };

      // there is nothing to sort at this time, but this is how I would make a new list matching your index....
      var myTests = (from x in myInt select new Tests(x, "whatever")).ToList();

      myTests.ForEach(i => Console.WriteLine("{0} {1}", i.iD, i.myString));


      // So assuming that you are starting with an unsorted list....
      // We create and priont one....
      myTests = new List<Tests>();
      for (int i = 0; i < myInt.Length; i++)
      {
        myTests.Add(new Tests(i, "number " + i));
      }

      Console.WriteLine("unsorted ==");
      myTests.ForEach(i => Console.WriteLine("{0} {1}", i.iD, i.myString));

      // And this will perform the sort based on your criteria.
      var sorted = (from x in myInt
                    from y in myTests
                    where y.iD == x
                    select y).ToList();

      // And output the results to prove it.
      Console.WriteLine("sorted ==");
      sorted.ForEach(i => Console.WriteLine("{0} {1}", i.iD, i.myString));

      Console.Read();
    }
  }

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.