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 of a string column

sorting of a string column

Scheduled Pinned Locked Moved C#
databasealgorithmshelpcode-review
8 Posts 6 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.
  • S Offline
    S Offline
    Sonia Gupta
    wrote on last edited by
    #1

    I have the column of type string in the database Following is the data in that column 1 2 11 12 21 abc If i sort the table with the help of this column then the output come in the following manner 1 11 12 2 21 abc can i improve the order actually i want the output like 1 2 11 12 21 abc

    Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

    L P L L P 5 Replies Last reply
    0
    • S Sonia Gupta

      I have the column of type string in the database Following is the data in that column 1 2 11 12 21 abc If i sort the table with the help of this column then the output come in the following manner 1 11 12 2 21 abc can i improve the order actually i want the output like 1 2 11 12 21 abc

      Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

      L Offline
      L Offline
      Laddie
      wrote on last edited by
      #2

      Why not add order by [column name] ASC

      Thanks Laddie Kindly rate if the answer was helpful

      1 Reply Last reply
      0
      • S Sonia Gupta

        I have the column of type string in the database Following is the data in that column 1 2 11 12 21 abc If i sort the table with the help of this column then the output come in the following manner 1 11 12 2 21 abc can i improve the order actually i want the output like 1 2 11 12 21 abc

        Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        You could, but only with some extremely fancy logic. It's a string, so you have to abide by the rules of strings. Off the top of my head, I would think that you would have to remove the numeric type strings and sort them independently of the non-numeric strings and then combine the two together.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        S 1 Reply Last reply
        0
        • P Pete OHanlon

          You could, but only with some extremely fancy logic. It's a string, so you have to abide by the rules of strings. Off the top of my head, I would think that you would have to remove the numeric type strings and sort them independently of the non-numeric strings and then combine the two together.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          S Offline
          S Offline
          Sonia Gupta
          wrote on last edited by
          #4

          i can use the union query to first select the numeric and then an another union query to sort alphanumeric. Am i doing right?

          Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

          P 1 Reply Last reply
          0
          • S Sonia Gupta

            i can use the union query to first select the numeric and then an another union query to sort alphanumeric. Am i doing right?

            Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Sonia Gupta wrote:

            i can use the union query to first select the numeric and then an another union query to sort alphanumeric. Am i doing right?

            For the love of God Sonia, just try it. I'm not going to type your code out for you.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            1 Reply Last reply
            0
            • S Sonia Gupta

              I have the column of type string in the database Following is the data in that column 1 2 11 12 21 abc If i sort the table with the help of this column then the output come in the following manner 1 11 12 2 21 abc can i improve the order actually i want the output like 1 2 11 12 21 abc

              Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

              L Offline
              L Offline
              Leyu
              wrote on last edited by
              #6

              Convert the data type of the column on the fly using convert or cast then reorder the converted column using ORDER BY. Make sure the converted type is integer!

              1 Reply Last reply
              0
              • S Sonia Gupta

                I have the column of type string in the database Following is the data in that column 1 2 11 12 21 abc If i sort the table with the help of this column then the output come in the following manner 1 11 12 2 21 abc can i improve the order actually i want the output like 1 2 11 12 21 abc

                Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

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

                Hi, I don't think SQL offers the required ordering. The "natural sort order" as used for file names by Windows Explorer is available through P/Invoke, so you can implement an IComparer interface like this:

                using System.Runtime.InteropServices; // DllImport

                /// <summary>
                /// Compares two strings while ignoring case and treating sequences of digits as numbers.
                /// (static version).
                /// </summary>
                public static int Compare(string s1, string s2) {
                if (s1!=null && s2!=null) return StrCmpLogicalW(s1, s2);
                else return string.Compare(s1, s2, true);
                }

                [DllImport("shlwapi.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
                private static extern int StrCmpLogicalW(string s1, string s2);

                I do not know how you could apply the above to database-based data; I would guess you need to first fill a DataTable, then somehow get that sorted using the IComparer object. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                1 Reply Last reply
                0
                • S Sonia Gupta

                  I have the column of type string in the database Following is the data in that column 1 2 11 12 21 abc If i sort the table with the help of this column then the output come in the following manner 1 11 12 2 21 abc can i improve the order actually i want the output like 1 2 11 12 21 abc

                  Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

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

                  Ordinarily I'd say left pad the numerics with SPACEs then sort, but SQL Server doesn't appear to have a PAD function? :wtf: I thought Oracle did. What Database system are you using? Some ideas for SQL Server:

                  select * from PadTest order by
                  REPLICATE(' ',8-LEN(X))+X

                  or

                  select * from PadTest order by
                  case ISNUMERIC(X)
                  when 1 then REPLICATE(' ',8-LEN(X))+X
                  else X end

                  modified on Friday, March 7, 2008 12:57 PM

                  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