Function that compares 2 alpha-numeric values?

Discussion in 'Javascript' started by success_ny@yahoo.com, Jul 21, 2005.

  1. Guest

    Does anyone have a code snippet to compare those values so I can sort
    the array of alpha-numeric values that include both characters and
    integers in it?

    I.e., if we have values like 4236 and 123234, I want 4236 to be second
    because 4 is bigger than 1 rather than using the numeric comparison.
    The strings can include character values and strings. Basically, I have
    the bubble sort function, the question is how to compare those types of
    strings in the alpha-numeric order.

    i.e.,

    A83745
    B34974
    127734
    34456
    788

    I looked all over the web thinking that this simple question would be
    answered somewhere, but could not find the answer. Please help.

    Thanks!
    , Jul 21, 2005
    #1
    1. Advertising

  2. VK Guest

    Re:

    wrote:
    > Does anyone have a code snippet to compare those values so I can sort
    > the array of alpha-numeric values that include both characters and
    > integers in it?
    >
    > I.e., if we have values like 4236 and 123234, I want 4236 to be second
    > because 4 is bigger than 1 rather than using the numeric comparison.
    > The strings can include character values and strings. Basically, I have
    > the bubble sort function, the question is how to compare those types of
    > strings in the alpha-numeric order.
    >
    > i.e.,
    >
    > A83745
    > B34974
    > 127734
    > 34456
    > 788
    >
    > I looked all over the web thinking that this simple question would be



    The time I wrote this script (a while ago) I was crying for Perl with
    his "cmp" and shuttle <=> Get's really "if'y" in JavaScript w/o them
    :)


    function alfanum(a,b) {
    var result = 0;
    if ((typeof(a)=='number')&&(typeof(b)=='number')) {
    result = a-b;
    }
    else if ((typeof(a)=='string')&&(typeof(b)=='string')) {
    if (a < b) {result = -1;}
    else if (a > b) {result = 1;}
    else {result = 0;}
    }
    else if (typeof(a)=='string'){
    result = 1;
    }
    else {
    result = -1
    }
    return result;
    }

    var arr = [9,'A',8,'B',7,'C',6,'D',5,'E',4,'F',3,'G',2,'H',1];

    alert(arr.sort(alfanum));
    VK, Jul 21, 2005
    #2
    1. Advertising

  3. JRS: In article <>
    , dated Thu, 21 Jul 2005 08:42:47, seen in news:comp.lang.javascript,
    posted :
    >Does anyone have a code snippet to compare those values so I can sort
    >the array of alpha-numeric values that include both characters and
    >integers in it?
    >
    >I.e., if we have values like 4236 and 123234, I want 4236 to be second
    >because 4 is bigger than 1 rather than using the numeric comparison.
    >The strings can include character values and strings. Basically, I have
    >the bubble sort function, the question is how to compare those types of
    >strings in the alpha-numeric order.
    >
    >i.e.,
    >
    >A83745
    >B34974
    >127734
    >34456
    >788
    >
    >I looked all over the web thinking that this simple question would be
    >answered somewhere, but could not find the answer. Please help.


    As you've looked all over the Web I assume that there is no need to
    repeat what you found on my Web site.

    The default array sort would be to treat letters as bigger than digits;
    no added code would be needed. That would give 12 34 78 A8 B3. You
    don't need to write and download a sort algorithm. Use that order if
    you can.

    How can a string include a string?

    You need to do one of two things : write a comparison function for
    array.sort to use, or transform your data so that it will sort in the
    right order, de-transforming afterwards. Since array.sort on a list of
    N items will call the sort function >0(N) times, you should transform
    the data if N is large.

    The simplest transform might be to take each character of a string in
    turn (I assume less than 240 possibilities), look up its rank in a
    priority list, encode that as two Hex characters after adding 16, pad
    with a "character" 00, and append the original data so that de-
    transformation is just a substring operation.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jul 21, 2005
    #3
  4. askMe Guest

    wrote:
    > Does anyone have a code snippet to compare those values so I can sort
    > the array of alpha-numeric values that include both characters and
    > integers in it?
    >
    > I.e., if we have values like 4236 and 123234, I want 4236 to be second
    > because 4 is bigger than 1 rather than using the numeric comparison.
    > The strings can include character values and strings. Basically, I have
    > the bubble sort function, the question is how to compare those types of
    > strings in the alpha-numeric order.
    >
    > i.e.,
    >
    > A83745
    > B34974
    > 127734
    > 34456
    > 788
    >
    > I looked all over the web thinking that this simple question would be
    > answered somewhere, but could not find the answer. Please help.
    >
    > Thanks!


    Try a loop over the whole set first to separate the numeric from the
    alphas with isNan. Run a sort on the numbers array, then a sort on the
    alpha array. Add the sort alpha array elements to the end of the
    numeric array.

    http://www.askblax.com
    askMe, Jul 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders

    Re: alpha numeric sorting for dataview

    John Saunders, Jul 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,676
    John Saunders
    Jul 20, 2004
  2. =?Utf-8?B?RWQ=?=
    Replies:
    0
    Views:
    3,258
    =?Utf-8?B?RWQ=?=
    Apr 18, 2006
  3. shan

    declaring alpha numeric

    shan, Nov 16, 2005, in forum: C Programming
    Replies:
    11
    Views:
    874
    Simon Biber
    Nov 17, 2005
  4. Replies:
    1
    Views:
    1,300
    Richard Senior
    Apr 24, 2007
  5. Replies:
    5
    Views:
    5,885
    Daniele Futtorovic
    Aug 19, 2008
Loading...

Share This Page