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. Strange List<string> sorting request</string>

Strange List<string> sorting request</string>

Scheduled Pinned Locked Moved C#
csharplinqalgorithmsregextutorial
5 Posts 4 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.
  • V Offline
    V Offline
    Vodstok
    wrote on last edited by
    #1

    Is it possible to sort a generic list base don the sort of another generic list? Here is what I am working with: I have a List object that I built out of a List, where the strings in the first list are populated with a specific string property from the second. so, say this is my example, the List<Q_object> object is made up of this: lets say Q_object has the follwing properties: string str1 string str2 int i and my objects in the list are specifically: Q_object[0] has str1 = "fudge"; str2 = "brownie"; i = 0; Q_object[1] has str1 = "Beer"; str2 = "Sausage"; i = 1; Q_object[2] has str1 = "Pancakes"; str2 = "waffles"; i = 2; now, the List has the following: "Sausage" "brownie" "waffles" essentially, somwhere along the line, the list got out of order. Now, I need it to be in the original order, but it has to be the string list as opposed to me trying to access the properties of each individual object in the original list. is there a way to say "sort List to match List's properties with the same values"? I'm thinking Linq has my answers, but I dont know for sure, and I dont know where to begin. Any suggestions would be appreciated :)

    ______________________ Oh Hamburgers!

    E G 2 Replies Last reply
    0
    • V Vodstok

      Is it possible to sort a generic list base don the sort of another generic list? Here is what I am working with: I have a List object that I built out of a List, where the strings in the first list are populated with a specific string property from the second. so, say this is my example, the List<Q_object> object is made up of this: lets say Q_object has the follwing properties: string str1 string str2 int i and my objects in the list are specifically: Q_object[0] has str1 = "fudge"; str2 = "brownie"; i = 0; Q_object[1] has str1 = "Beer"; str2 = "Sausage"; i = 1; Q_object[2] has str1 = "Pancakes"; str2 = "waffles"; i = 2; now, the List has the following: "Sausage" "brownie" "waffles" essentially, somwhere along the line, the list got out of order. Now, I need it to be in the original order, but it has to be the string list as opposed to me trying to access the properties of each individual object in the original list. is there a way to say "sort List to match List's properties with the same values"? I'm thinking Linq has my answers, but I dont know for sure, and I dont know where to begin. Any suggestions would be appreciated :)

      ______________________ Oh Hamburgers!

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      You can write a custom comparer for the object to be used in the list? I don't really understand the problem but sometimes that is a good solution for sorting woes, There is also the SortedList class available for use.

      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
      Most of this sig is for Google, not ego.

      1 Reply Last reply
      0
      • V Vodstok

        Is it possible to sort a generic list base don the sort of another generic list? Here is what I am working with: I have a List object that I built out of a List, where the strings in the first list are populated with a specific string property from the second. so, say this is my example, the List<Q_object> object is made up of this: lets say Q_object has the follwing properties: string str1 string str2 int i and my objects in the list are specifically: Q_object[0] has str1 = "fudge"; str2 = "brownie"; i = 0; Q_object[1] has str1 = "Beer"; str2 = "Sausage"; i = 1; Q_object[2] has str1 = "Pancakes"; str2 = "waffles"; i = 2; now, the List has the following: "Sausage" "brownie" "waffles" essentially, somwhere along the line, the list got out of order. Now, I need it to be in the original order, but it has to be the string list as opposed to me trying to access the properties of each individual object in the original list. is there a way to say "sort List to match List's properties with the same values"? I'm thinking Linq has my answers, but I dont know for sure, and I dont know where to begin. Any suggestions would be appreciated :)

        ______________________ Oh Hamburgers!

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        What is the reason that you need to sort the list of strings? If it should just be a copy of some of the data in the list of objects, can't you just recreate the list? Do you actually have to create the list of strings, can't you just read the data from the list of objects?

        Despite everything, the person most likely to be fooling you next is yourself.

        V 1 Reply Last reply
        0
        • G Guffa

          What is the reason that you need to sort the list of strings? If it should just be a copy of some of the data in the list of objects, can't you just recreate the list? Do you actually have to create the list of strings, can't you just read the data from the list of objects?

          Despite everything, the person most likely to be fooling you next is yourself.

          V Offline
          V Offline
          Vodstok
          wrote on last edited by
          #4

          the logic being used is very flexible and was designed to grab strings from any properties contained in a list of any objects. if you know the properties of the object (and you should at least one, otherwise you shouldn't be using it ;))and return the valu eof that property in each instance of the object in the list. Its hard to read, I know, but it works. the issue is that somewhere along the line, the data is getting out of sort, and I dont have time (yet) to add logic for sorting into the method. (it would have to identify the other property in the object to sort from, if such a property exists, blah blah blah) Why wouldnt i just access the property of each individual object in the list? Believe it or not, this was written to run through huge XML strings and grab the potential instances of object descriptions contained within, and then return the property values, if the objects exist, and if their properties arent null. I didnt design it, but I have to use it, so this method ignores the "what if" and just returns what is. And now i have to work with what comes out th eother side.

          ______________________ Oh Hamburgers!

          M 1 Reply Last reply
          0
          • V Vodstok

            the logic being used is very flexible and was designed to grab strings from any properties contained in a list of any objects. if you know the properties of the object (and you should at least one, otherwise you shouldn't be using it ;))and return the valu eof that property in each instance of the object in the list. Its hard to read, I know, but it works. the issue is that somewhere along the line, the data is getting out of sort, and I dont have time (yet) to add logic for sorting into the method. (it would have to identify the other property in the object to sort from, if such a property exists, blah blah blah) Why wouldnt i just access the property of each individual object in the list? Believe it or not, this was written to run through huge XML strings and grab the potential instances of object descriptions contained within, and then return the property values, if the objects exist, and if their properties arent null. I didnt design it, but I have to use it, so this method ignores the "what if" and just returns what is. And now i have to work with what comes out th eother side.

            ______________________ Oh Hamburgers!

            M Offline
            M Offline
            Mbah Dhaim
            wrote on last edited by
            #5

            As ennis said you have to make comparer by your self see this link[^]

            dhaim programming is a hobby that make some money as side effect :)

            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