0

How to write sorting for date and name in ? I have one table. There are 2 columns: name and created date:

name:  ["A", "A9", "A10", "A11", "A3"]
createdDate: ["Apr 2, 2019 3:07 PM", "Apr 10, 2019 9:25 AM", "Apr 30, 2019 6:08 PM", "Apr 10, 2019 8:25 AM", "Apr 2, 2019 8:07 PM"]

I already try using sort method. Output should like this:

name: ["A", "A3", "A9", "A10", "A11"]
createdDate: ["Apr 2, 2019 3:07 PM", "Apr 2, 2019 8:07 PM","Apr 10, 2019 8:25 AM", "Apr 10, 2019 9:25 AM", "Apr 30, 2019 6:08 PM"]
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
  • 2
    Possible duplicate of [How to define custom sort function in javascript?](https://stackoverflow.com/questions/5002848/how-to-define-custom-sort-function-in-javascript) – Mike Doe May 02 '19 at 06:23
  • Actually its not working for alphanumeric character. i already try by below code :- .sort(a,b){ var firstEl = (a === undefined) ? a :a.toUpperCase(); var secondEl = (b === undefined) ? b: b.toUpperCase(); if(firstEl === undefined || firstEl === "") {return (1)} if(secondEl === undefined || secondEl === "") {return (-1)} if(firstEl === secondEl) {return 0;} return firstEl < secondEl ? (-1) : (1 ); } – kashif azmi May 02 '19 at 06:28
  • should the sorting of `name` affect `createdDate` and vice versa? – Nina Scholz May 02 '19 at 07:21
  • Yes its should be – kashif azmi May 02 '19 at 08:25

3 Answers3

0

To sort strings :

array.sort(function(a, b) {
    var titleA = a.title.toLowerCase(), titleB = b.title.toLowerCase();
    if (titleA < titleB) return -1;
    if (titleA > titleB) return 1;
    return 0;
});

To sort dates :

array.sort(function(a, b) {
    var dateA = new Date(a.release), dateB = new Date(b.release);
    return dateA - dateB;
});

Refer to this document https://en.proft.me/2015/11/14/sorting-array-objects-number-string-date-javascrip/ for more details.

sudip
  • 2,781
  • 1
  • 29
  • 41
0

This is how you can sort the name array alphanumerically, using localeCompare(), and passing numeric as one of the options.

const name =  ["A", "A9", "A10", "A11", "A3"];
name.sort((a, b) => a.localeCompare(b, 'en', { numeric: true }));
console.log(name);

Here is how you can sort them by dates. Essentially, we convert them to JavaScript date objects when doing the comparison.

const createdDate = ["Apr 2, 2019 3:07 PM", "Apr 2, 2019 8:07 PM","Apr 10, 2019 8:25 AM", "Apr 10, 2019 9:25 AM", "Apr 30, 2019 6:08 PM"];
createdDate.sort((a, b) => new Date(a) - new Date(b));
console.log(createdDate);
wentjun
  • 40,384
  • 10
  • 95
  • 107
  • but i do have combination of string and alphanumeric character and its not working – kashif azmi May 02 '19 at 06:54
  • Just to check, did you try the above method with that name array you specified? `["A", "A9", "A10", "A11", "A3"]`? Because when I tested it out locally, it gives me this `["A", "A3", "A9", "A10", "A11"]`, which is actually correct? – wentjun May 02 '19 at 06:56
  • yeah! already tried but i have new test case like name contain :- name: ["a", "a10", "a11", "A9", "Atlas", "btlas"] is this case its not working – kashif azmi May 02 '19 at 07:13
  • now i have added condition for alphanumeric character but i have one doubt is above method always working fine for date ? – kashif azmi May 02 '19 at 07:26
  • @kashifazmi I have updated my answer to check for sorting of dates. Do try it out? :) – wentjun May 02 '19 at 08:30
0

to do it properly I'd combine the answers of sudip and wentjun

strings:

name.sort((a, b) => a.localeCompare(b, 'en', { numeric: true }));

dates:

createdDate.sort((a, b) => Date.parse(a) - Date.parse(b));
nilsolofsson
  • 96
  • 2
  • 6