Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. sorting

sorting

Scheduled Pinned Locked Moved C#
algorithmsquestion
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Ramkithepower

    Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?

    Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

    R Offline
    R Offline
    rvdenden
    wrote on last edited by
    #3

    using System.Linq; public class record { public string name1; public string name2; } List records = new List(); records.Add(new record() { name1 = "a", name2 = "b" }); records.Add(new record() { name1 = "c", name2 = "d" }); records.Add(new record() { name1 = "e", name2 = "f" }); records.Add(new record() { name1 = "g", name2 = "h" }); records.Add(new record() { name1 = "i", name2 = "j" }); var r = records.OrderByDescending(sort => sort.name1).OrderBy(sort2 => sort2.name2);;

    S 1 Reply Last reply
    0
    • R Ramkithepower

      Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?

      Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

      N Offline
      N Offline
      Nuri Ismail
      wrote on last edited by
      #4

      There are lots of ways of sorting the list: see here[^]. :) I would suggest you implement the IComparable<T>[^] interface and provide an appropriate body for CompareTo method. You can find such an example here[^].

      Ramkithepower wrote:

      Now I have to sort this class first by name1 and then by name2.

      In this case a possible implementation of the CompareTo method could be:

      public int CompareTo(Record other)
      {
      int res = name1.CompareTo(other.name1);

      if(res == 0)
      res = name2.CompareTo(other.name2);

      return res;
      }

      1 Reply Last reply
      0
      • R Ramkithepower

        Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?

        Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

        S Offline
        S Offline
        Simon P Stevens
        wrote on last edited by
        #5

        You can use OrderBy[^]

        var sortedData = records.OrderBy(r => r.name1 + r.name2);

        You just concatenate the names in the key selector to achieve the ordering you want. Opps. PIEBALDconsult[^] is of course totally right. That was a very stupid suggestion of mine, I don't know what I was thinking. Please ignore the above suggestion. This is how you should really do it (using ThenBy[^]):

        var sortedData = records.OrderBy(sort => sort.name1).ThenBy(sort2 => sort2.name2);

        Simon

        P L R 3 Replies Last reply
        0
        • S Simon P Stevens

          You can use OrderBy[^]

          var sortedData = records.OrderBy(r => r.name1 + r.name2);

          You just concatenate the names in the key selector to achieve the ordering you want. Opps. PIEBALDconsult[^] is of course totally right. That was a very stupid suggestion of mine, I don't know what I was thinking. Please ignore the above suggestion. This is how you should really do it (using ThenBy[^]):

          var sortedData = records.OrderBy(sort => sort.name1).ThenBy(sort2 => sort2.name2);

          Simon

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #6

          I don't think that's reliable.

          S 1 Reply Last reply
          0
          • R rvdenden

            using System.Linq; public class record { public string name1; public string name2; } List records = new List(); records.Add(new record() { name1 = "a", name2 = "b" }); records.Add(new record() { name1 = "c", name2 = "d" }); records.Add(new record() { name1 = "e", name2 = "f" }); records.Add(new record() { name1 = "g", name2 = "h" }); records.Add(new record() { name1 = "i", name2 = "j" }); var r = records.OrderByDescending(sort => sort.name1).OrderBy(sort2 => sort2.name2);;

            S Offline
            S Offline
            Simon P Stevens
            wrote on last edited by
            #7

            rvdenden wrote:

            var r = records.OrderByDescending(sort => sort.name1).OrderBy(sort2 => sort2.name2);;

            This isn't correct.

            MSDN - OrderBy[^]:

            Because IOrderedEnumerable inherits from IEnumerable, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new primary ordering that ignores the previously established ordering.

            You need to use ThenBy[^] for the second stage of the sort.

            var r = records.OrderByDescending(sort => sort.name1).ThenBy(sort2 => sort2.name2);

            Simon

            1 Reply Last reply
            0
            • P PIEBALDconsult

              I don't think that's reliable.

              S Offline
              S Offline
              Simon P Stevens
              wrote on last edited by
              #8

              Why?

              Simon

              P L 2 Replies Last reply
              0
              • R Ramkithepower

                Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?

                Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #9

                This article[^] covers the regular ways of sorting; it should be easy to understand and offer good performance at the same time; it does not offer the latest LINQ fancy things. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                1 Reply Last reply
                0
                • S Simon P Stevens

                  Why?

                  Simon

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #10

                  Unless you right-pad name1. Or are you counting on case-sensitivity?

                  S 1 Reply Last reply
                  0
                  • S Simon P Stevens

                    You can use OrderBy[^]

                    var sortedData = records.OrderBy(r => r.name1 + r.name2);

                    You just concatenate the names in the key selector to achieve the ordering you want. Opps. PIEBALDconsult[^] is of course totally right. That was a very stupid suggestion of mine, I don't know what I was thinking. Please ignore the above suggestion. This is how you should really do it (using ThenBy[^]):

                    var sortedData = records.OrderBy(sort => sort.name1).ThenBy(sort2 => sort2.name2);

                    Simon

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    Suppose name1 is the empty string..? edit: ok you changed it

                    modified on Thursday, May 27, 2010 9:47 AM

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Unless you right-pad name1. Or are you counting on case-sensitivity?

                      S Offline
                      S Offline
                      Simon P Stevens
                      wrote on last edited by
                      #12

                      Ohh... You are very right. Thanks. Will edit.

                      Simon

                      1 Reply Last reply
                      0
                      • S Simon P Stevens

                        Why?

                        Simon

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #13

                        because it is wrong. consider the following set of string pairs:

                        a,bd
                        ab,c

                        and try sorting them the right way and the concatenated way. It would be right if you were to use string1 + charMax + string2 where charMax is a character that sorts last in the character collection. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read formatted code with indentation, so please use PRE tags for code snippets.


                        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                        1 Reply Last reply
                        0
                        • S Simon P Stevens

                          You can use OrderBy[^]

                          var sortedData = records.OrderBy(r => r.name1 + r.name2);

                          You just concatenate the names in the key selector to achieve the ordering you want. Opps. PIEBALDconsult[^] is of course totally right. That was a very stupid suggestion of mine, I don't know what I was thinking. Please ignore the above suggestion. This is how you should really do it (using ThenBy[^]):

                          var sortedData = records.OrderBy(sort => sort.name1).ThenBy(sort2 => sort2.name2);

                          Simon

                          R Offline
                          R Offline
                          Ramkithepower
                          wrote on last edited by
                          #14

                          Thank you all for the reply.

                          Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups