locked
Ordering a list in a particular sequence RRS feed

  • Question

  • User638129481 posted

    Hi,

    Say I have a list that looks like this:

    231 213 123 331 322 123

    And I want to order it in sequences of 123. Such that it looks like this: 

    123 123 123 123 123 123 

    The value 123 is an object called (Seq)

    How would I go about doing this? Can I split the list and re order it, or can I just keep using OrderBy on it?

    Wednesday, October 7, 2015 8:16 AM

Answers

  • User-434868552 posted

    @justatkinson...

    is the a HOMEWORK question?

    Your example is flawed:

    231 213 123 331 322 123
                ^^
    123 123 123 123 123 123 

    the solution is very trivial which is why it looks like a HOMEWORK problem.

    Missing from your example is the WHAT, i.e., is this a list of Int32, String, ... ?

    TIMTOWTDI

    Here's one way; try to write the code yourself:

    assuming it's a List<Int32>, iterate over your list.

    for each item in your list, convert it to an array of System.Char:

         Int32 i = 321;
         String s = i.ToString();
         Char[] c = s.ToCharArray();
         Array.Sort(c);
         String sc = new String(c);

    edit:

    you will need to convert the string back to an Int32  -- study https://msdn.microsoft.com/en-us/library/bb397679.aspx "How to: Convert a String to a Number (C# Programming Guide)"

    end edit.

    as for your list, you could create a new list:

    List<Int32> iList   = new List<Int32> { 321, 231};
    List<Int32> newLIst = new List<Int32>();
    Int32 iTemp;
    foreach (Int32 number in iList)
    {
        iTemp = number;
        // ... change each iTemp to      123
        newLIst.Add(iTemp);
    }

    justatkinson..., please make an effort to put the above pieces together and to understand what you are doing.

    feel free to ask for more help after you've given this your best effort.

    also, if this is HOMEWORK, talk to your teacher.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2015 9:58 AM