1

I have an array something like:

['honda accord', '1986 honda accord', '1990 honda accord', '2000 honda accord', '2011 honda accord', '2016 honda accord'].

I need to sort this in javascript so that the result is:

['honda accord', '2016 honda accord', '2011 honda accord', '2000 honda accord', '1990 honda accord', '1986 honda accord'].

how can i do this.

array.sort() 

does not work. I know i need to send a function to sort.

Thanks Brian

4 Answers 4

2

You could use split and check first item of the string

var arr = ['honda accord', '1986 honda accord', '1990 honda accord', '2000 honda accord', '2011 honda accord', '2016 honda accord'];

arr.sort((a, b) => b.split(' ')[0] - a.split(' ')[0])

document.write(JSON.stringify(arr));

Sign up to request clarification or add additional context in comments.

Comments

1

Working Fiddle : https://jsfiddle.net/7nu7bp3g/

var arr = [
          'honda accord',
          '1986 honda accord',
          '1990 honda accord',
          '2000 honda accord',
          '2011 honda accord',
          '2016 honda accord'
          ];

var sortArr = arr.sort(function(a, b) {
return b.split(' ')[0] - a.split(' ')[0];
});

console.log(sortArr);

I hope it will work.

Comments

0

It is not very elegant (or efficient) but it works. Essentially it uses Regular Expression to extract the year from each car and then compare.

// the regular expression to extract the year and car
var regEx = /((\d+)\s)*(.*)/;

var list = ['honda accord', '1986 honda accord', '1990 honda accord', '2000 honda accord', '2011 honda accord', '2016 honda accord'];

// custom sort function
list.sort(function(a, b)
{
    // get the car details
    var aR = regEx.exec(a);
    var bR = regEx.exec(b);

    var aCar = aR[3];
    var bCar = bR[3];

    var aYear = aR[2];
    var bYear = bR[2];

    // compare the strings
    if(aCar < bCar) return -1;
    else if(aCar > bCar) return 1;
    // we'll only get this far if both strings are the same

    // if there was no year provided then it should be first
    else if(!aYear) return -1;
    else if(!bYear) return 1;

    // at this point the car types are the same, and both have an year so compare the year
    else if(aYear < bYear) return 1;
    else if(aYear > bYear) return -1;
    return 0;
})

You could make it more efficient by pre-processing the list to run the regex on all of the items.

Comments

0

A solution with replace if no year is given

var array = ['honda accord', '1986 honda accord', '1990 honda accord', '2000 honda accord', '2011 honda accord', '2016 honda accord'];

array.sort((a, b) => b.replace(/^\D*$/g, '9999 $1').localeCompare(a.replace(/^\D*$/g, '9999 $1')));

document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.