Solved

alphanumeric value sorting

Posted on 2015-01-20
8
230 Views
Last Modified: 2015-01-29
How to do alphanumeric value sorting in javascript  or jquery

A10
B100
A2
AA1
AD200
32
11
A1

and the i want the result should be
A1
A2
A10
B2
B100
AD200
11
32
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 4

Assisted Solution

by:Monika Bharti
Monika Bharti earned 50 total points
ID: 40559271
This will give proper sorting for values like A1, A2, A3, etc as well as values without a street number.

function naturalSorter(as, bs){
    var a, b, a1, b1, i= 0, n, L,
    rx=/(\.\d+)|(\d+(\.\d+)?)|([^\d.]+)|(\.\D+)|(\.$)/g;
    if(as=== bs) return 0;
    a= as.toLowerCase().match(rx);
    b= bs.toLowerCase().match(rx);
    L= a.length;
    while(i<L){
        if(!b[i]) return 1;
        a1= a[i],
        b1= b[i++];
        if(a1!== b1){
            n= a1-b1;
            if(!isNaN(n)) return n;
            return a1>b1? 1:-1;
        }
    }
    return b[i]? -1:0;
}

Open in new window

Then
arr.sort(function(o1, o2) {
    return naturalSorter(o1.t, o2.t);
});

Open in new window

0
 
LVL 15

Expert Comment

by:navneethegde
ID: 40559305
HI!

Check if this helps.
<script>

var testCode = ['A10','B100','A2','AA1','AD200','32','11','A1'];
var rte = testCode.sort();

document.write(rte);
</script>

Open in new window





Thanks!
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40559390
@navneethegde please check this test code .

var testCode = ['A10','B100','A2','AA1','AD200','32','11','1','2','10','100','A1'];

Thanks @Monika but its sort number correctly but not alphanumeric value
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 5

Assisted Solution

by:GuruJava
GuruJava earned 50 total points
ID: 40559395
arr.sort(function(o1, o2) {
           var a = o1.t.split(/(\d+|\D+)/).filter(function(s){return s!=""});
           var b = o2.t.split(/(\d+|\D+)/).filter(function(s){return s!=""});
           for (var cmp = 0, i = 0; 0 == cmp && i < a.length && i < b.length; i++) {
             var n1 = a[i] - 0, n2 = b[i] - 0;
             if (!isNaN(n1) && !isNaN(n2)) 
               cmp = n1 - n2;
             else if (a[i] < b[i]) 
               cmp = -1;
             else if (a[i] > b[i])
               cmp = 1;
           }
           return cmp;
         })

Open in new window

0
 
LVL 15

Expert Comment

by:navneethegde
ID: 40559396
here it is
1,10,100,11,2,32,A1,A10,A2,AA1,AD200,B100

Open in new window

0
 
LVL 15

Accepted Solution

by:
navneethegde earned 350 total points
ID: 40559400
Hi!
You can try the below one as well
the result of below is : 1,2,10,11,32,100,A1,A2,A10,AA1,AD200,B100

<script>
var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;
function sortAlphaNum(a,b) {
    var aA = a.replace(reA, "");
    var bA = b.replace(reA, "");
    if(aA === bA) {
        var aN = parseInt(a.replace(reN, ""), 10);
        var bN = parseInt(b.replace(reN, ""), 10);
        return aN === bN ? 0 : aN > bN ? 1 : -1;
    } else {
        return aA > bA ? 1 : -1;
    }
}

var testCode = ['A10','B100','A2','AA1','AD200','32','11','1','2','10','100','A1'].sort(sortAlphaNum);

document.write(testCode);

</script>

Open in new window



Thanks!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 40561470
@navneethegde did you read the question?

and the I want the result should be
A1
A2
A10
B2
B100
AD200
11
32
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 50 total points
ID: 40561678
My suggestion

JSFIDDLE

function rxIndexOf(rx,arr) { for (var i in arr) {if (arr[i].toString().match(rx)) { return i; }} return -1; }
function splitAlp(str) { return {num  : str.replace(/[^0-9]+/ig,"")||"",alph : str.replace(/[0-9]/g,"") ||"" }}
function numSort(a,b) { if (a===b) return 0; return parseInt(a,10)>parseInt(b,10)?1:-1; }

function alpSort(a,b) {
    if (a===b) return 0;
    var a1 = splitAlp(a);
    var b1 = splitAlp(b);
    if (a1.alph>b1.alph && parseInt(a1.num,10)>parseInt(b1.num,10)) return 1;
    if (a1.alph==b1.alph && parseInt(a1.num,10)>parseInt(b1.num,10)) return 1;
    return -1;
}
var alp,num; 
var arr = "A10,B100,A2,AA1,2,AD200,32,11,A1".split(",").sort();
var idx = rxIndexOf(/[a-zA-Z]/,arr);
if (idx !=-1) {
    num = arr.slice(0,idx).sort(numSort);
    arr = arr.slice(idx).sort(alpSort);
}   
if (num) arr=arr.concat(num);

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
href return to normal window 5 28
ASP sending two datas 2 22
Pass SQL to JSON. Page is in classic ASP and using Jquery 4 35
javascript form - error when hitting enter 3 27
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question