Natural ordering in postgresql? Does it exist?

Ask a Question related to PostgreSQL / PGSQL, Design and Development.

  1. #1

    Default Natural ordering in postgresql? Does it exist?

    Hi all,
    I have a field that I'll be ordering and I noticed that ordering is done
    logically and would confuse my users here (1,12,16,4,8, etc).

    I'm writing an application in PHP that connects to Postgres and while I know
    that PHP has some powerful natural ordering functions it would be much
    easier if I could just use something from postgres directly. Does there
    exist any way to order naturally?

    Thanks,
    Clark Endrizzi

    __________________________________________________ _______________
    Is your PC infected? Get a FREE online computer virus scan from McAfee®
    Security. [url]http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963[/url]


    ---------------------------(end of broadcast)---------------------------
    TIP 7: don't forget to increase your free space map settings

    Clark Endrizzi Guest

  2. Similar Questions and Discussions

    1. #40418 [NEW]: PQfreemem does not exist prior to Postgresql 7.4
      From: wdierkes at 5dollarwhitebox dot org Operating system: Redhat EL 3 (i386 and x86_64) PHP version: 5.2.1 PHP Bug Type: ...
    2. want to pull natural gas trading prices into flashapplication
      I have developed a natural gas calculator that will figure up a persons intrest (royaltys) for a customer of mine. Currently the users will enter...
    3. Natural order sort
      I've written a natural order comparison funtion for the String class. This was based on Martin Pool's "Natural Order String Comparison" which was...
    4. Natural looking eyes?
      I'm having a problem with fixing the red eye (actually WHITE eye) on my pictures. How can I get natural looking eyes when the people have bright...
    5. Natural penis enlargement - jy0 dq618j wq h
      Oh, goodie!! I'll no longer need to sleep standing up with those pesky weights hanging off my 'john thomas'!!! (Didn't do much good, really, but...
  3. #2

    Default Re: Natural ordering in postgresql? Does it exist?

    "Clark Endrizzi" <clarkendrizzi@hotmail.com> writes:
    > Hi all,
    > I have a field that I'll be ordering and I noticed that ordering is
    > done logically and would confuse my users here (1,12,16,4,8, etc).
    Sounds like you're storing a number in a text field. Numeric fields
    sort in numerical order.

    -Doug

    ---------------------------(end of broadcast)---------------------------
    TIP 5: Have you checked our extensive FAQ?

    [url]http://www.postgresql.org/docs/faqs/FAQ.html[/url]

    Doug McNaught Guest

  4. #3

    Default Re: Natural ordering in postgresql? Does it exist?

    Numbers as text are ordered like that. Integers are ordered as you would like.

    The best hack I have seen if you are stuck with text is

    ....order by length(numbers_as_test), numbers_as_text

    which sorts first by number of "digits" then by text order.

    Ian Harding
    Programmer/Analyst II
    Tacoma-Pierce County Health Department
    [email]iharding@tpchd.org[/email]
    Phone: (253) 798-3549
    Pager: (253) 754-0002
    >>> "Clark Endrizzi" <clarkendrizzi@hotmail.com> 12/10/04 1:47 PM >>>
    Hi all,
    I have a field that I'll be ordering and I noticed that ordering is done
    logically and would confuse my users here (1,12,16,4,8, etc).

    I'm writing an application in PHP that connects to Postgres and while I know
    that PHP has some powerful natural ordering functions it would be much
    easier if I could just use something from postgres directly. Does there
    exist any way to order naturally?

    Thanks,
    Clark Endrizzi

    __________________________________________________ _______________
    Is your PC infected? Get a FREE online computer virus scan from McAfee®
    Security. [url]http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963[/url]


    ---------------------------(end of broadcast)---------------------------
    TIP 7: don't forget to increase your free space map settings


    ---------------------------(end of broadcast)---------------------------
    TIP 5: Have you checked our extensive FAQ?

    [url]http://www.postgresql.org/docs/faqs/FAQ.html[/url]

    Ian Harding Guest

  5. #4

    Default Re: Natural ordering in postgresql? Does it exist?

    On Fri, Dec 10, 2004 at 02:47:53PM -0700, Clark Endrizzi wrote:
    > I have a field that I'll be ordering and I noticed that ordering is done
    > logically and would confuse my users here (1,12,16,4,8, etc).
    I think you mean that the ordering is done lexically and you want
    it done numerically. If the fields are entirely numeric then storing
    them using one of the numeric types (INTEGER, NUMERIC, DOUBLE
    PRECISION, etc.) will result in numeric sort orders. If you have
    all-numeric values in VARCHAR/TEXT fields, then you can cast them
    to one of the numeric types in the ORDER BY clause:

    SELECT ...
    ORDER BY fieldname::INTEGER;

    If the values are a mixture of text and numbers (e.g., ABC-1, ABC-12,
    etc.) then you could use string functions to order different parts
    of the field differently:

    SELECT ...
    ORDER BY SUBSTRING(fieldname, 1, 3),
    SUBSTRING(fieldname, 5)::INTEGER;

    SELECT ...
    ORDER BY SUBSTRING(fieldname FROM '^(\\w+)'),
    SUBSTRING(fieldname FROM '(\\d+)')::INTEGER;

    --
    Michael Fuhr
    [url]http://www.fuhr.org/~mfuhr/[/url]

    ---------------------------(end of broadcast)---------------------------
    TIP 4: Don't 'kill -9' the postmaster

    Michael Fuhr Guest

  6. #5

    Default Re: Natural ordering in postgresql? Does it exist?

    Quoth [email]clarkendrizzi@hotmail.com[/email] ("Clark Endrizzi"):
    > I have a field that I'll be ordering and I noticed that ordering is
    > done logically and would confuse my users here (1,12,16,4,8, etc).
    >
    > I'm writing an application in PHP that connects to Postgres and while
    > I know that PHP has some powerful natural ordering functions it would
    > be much easier if I could just use something from postgres directly.
    > Does there exist any way to order naturally?
    If you wish to impose an ordering on an SQL query, you must specify
    that ordering using an "ORDER BY" clause.

    That's not a PostgreSQL issue; that's how SQL works.
    --
    output = ("cbbrowne" "@" "gmail.com")
    [url]http://linuxfinances.info/info/linux.html[/url]
    Rules of the Evil Overlord #204. "I will hire an entire squad of blind
    guards. Not only is this in keeping with my status as an equal
    opportunity employer, but it will come in handy when the hero becomes
    invisible or douses my only light source."
    <http://www.eviloverlord.com/>
    Christopher Browne Guest

  7. #6

    Thumbs up Re: Natural ordering in postgresql? Does it exist?

    Michael Fuhr, thank you!
    Andrey Zinchenko is offline Junior Member
    Join Date
    Nov 2011
    Posts
    1

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139