-1

I had the problem of sorting alphanumeric array.

sort pattern number comes first then letter

09312d27-4ddc-458e-95dd-74531f787368,
1edcdde8-702e-4703-a505-ca81b77accdd,
.....

Code:

var alphaNumericArray = ['8a337e46-7d3f-46bc-b661-579f8d25fe09',
'09312d27-4ddc-458e-95dd-74531f787368',
'e7c5fba1-ce59-439a-a560-8f558e7c0ac4',
'1edcdde8-702e-4703-a505-ca81b77accdd',
'2f66ba6c-7fd2-4d52-8ed6-b0ee6d4d0310',
'7529217d-ee80-41eb-ade7-208674924ab9',
'523f4f75-8d0a-4ad1-8f40-e8bc679956be',
'80871291-c8de-47d9-9b02-3f8444c5d64e',
'7529217d-ee80-41eb-ade7-208674924ab9'];

alphaNumericArray.sort(function(a, b) {
   a = a.value;
   b = b.value;
   return a-b;
});

//console.log(alphaNumericArray);
//alert(alphaNumericArray);

The output is not sorted. It shows me same array in alert popup and in console log.

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
  • 7
    Sorting in JavaScript is string-comparison sorting by default. You can't subtract one string from another, which is why your comparator function doesn't work. – Pointy Feb 05 '15 at 14:50
  • Also this has nothing to do with jQuery. – Pointy Feb 05 '15 at 14:51
  • 1
    `[...]jQuery is a popular cross-browser JavaScript library that facilitates DOM (HTML Structure) traversal, event handling, animations, and AJAX interactions [...]` your question is not related to jQuery at all. But is vanilla javascript. – t.niese Feb 05 '15 at 14:51
  • 2
    `return a.localeCompare(b);`, and the array has no `value` property ? – adeneo Feb 05 '15 at 14:52
  • are you sorting purley by along the lines of: numbers first (sorted as you'd expect) then letters (unsorted)? – atmd Feb 05 '15 at 14:56
  • 4
    why not alphaNumericArray.sort();? it sorts perfectly? – Cristian Olaru Feb 05 '15 at 14:56
  • Seems like `alphaNumericArray.sort();` should work fine. – rsbarro Feb 05 '15 at 14:57
  • Why are you trying to access `.value` on a string? Btw, `return parseInt(a.replace(/-/g,""), 16) - parseInt(b.replace(/-/g,""), 16)` should work if you want to compare *numerically*. – Bergi Feb 05 '15 at 15:03

1 Answers1

2

var alphaNumericArray = [
  '8a337e46-7d3f-46bc-b661-579f8d25fe09',
  '09312d27-4ddc-458e-95dd-74531f787368',
  'e7c5fba1-ce59-439a-a560-8f558e7c0ac4',
  '1edcdde8-702e-4703-a505-ca81b77accdd',
  '2f66ba6c-7fd2-4d52-8ed6-b0ee6d4d0310',
  '7529217d-ee80-41eb-ade7-208674924ab9',
  '523f4f75-8d0a-4ad1-8f40-e8bc679956be',
  '80871291-c8de-47d9-9b02-3f8444c5d64e',
  '7529217d-ee80-41eb-ade7-208674924ab9'
];

alphaNumericArray.sort();

document.getElementById('out').textContent = JSON.stringify(alphaNumericArray, null, 2);
<pre id="out"></pre>

the result is:

[
  "09312d27-4ddc-458e-95dd-74531f787368",
  "1edcdde8-702e-4703-a505-ca81b77accdd",
  "2f66ba6c-7fd2-4d52-8ed6-b0ee6d4d0310",
  "523f4f75-8d0a-4ad1-8f40-e8bc679956be",
  "7529217d-ee80-41eb-ade7-208674924ab9",
  "7529217d-ee80-41eb-ade7-208674924ab9",
  "80871291-c8de-47d9-9b02-3f8444c5d64e",
  "8a337e46-7d3f-46bc-b661-579f8d25fe09",
  "e7c5fba1-ce59-439a-a560-8f558e7c0ac4"
]

read more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Cristian Olaru
  • 487
  • 5
  • 20
  • 1
    This is the correct solution, but please also explain why it works (and the OP's attempt didn't) – Bergi Feb 05 '15 at 15:01
  • please read here http://www.w3schools.com/jsref/jsref_sort.asp – Cristian Olaru Feb 05 '15 at 15:03
  • 1
    better not, use [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) as a reference. However I'm suggesting that you [edit] your answer to incorporate the explanation :-) – Bergi Feb 05 '15 at 15:04