Sort array by firstname (alphabetically) in Javascript

前端 未结 23 2638
天命终不由人
天命终不由人 2020-11-22 11:46

I got an array (see below for one object in the array) that I need to sort by firstname using JavaScript. How can I do it?

var user = {
   bio: null,
   emai         


        
相关标签:
23条回答
  • 2020-11-22 12:14

    try

    users.sort((a,b)=> (a.firstname>b.firstname)*2-1)
    

    var users = [
      { firstname: "Kate", id: 318, /*...*/ },
      { firstname: "Anna", id: 319, /*...*/ },
      { firstname: "Cristine", id: 317, /*...*/ },
    ]
    
    console.log(users.sort((a,b)=> (a.firstname>b.firstname)*2-1) );

    0 讨论(0)
  • 2020-11-22 12:14

    Just for the record, if you want to have a named sort-function, the syntax is as follows:

    let sortFunction = (a, b) => {
     if(a.firstname < b.firstname) { return -1; }
     if(a.firstname > b.firstname) { return 1; }
     return 0;
    })
    users.sort(sortFunction)
    

    Note that the following does NOT work:

    users.sort(sortFunction(a,b))
    
    0 讨论(0)
  • 2020-11-22 12:14

    I'm surprised no one mentioned Collators. You shouldn't use localeCompare unless you have to as it has significantly worse performance.

    const collator = new Intl.Collator('zh-CN', { // Chinese Simplified for example
      numeric: true,
      sensitivity: 'base',
    });
    
    function sortAsc(a, b) {
      if (typeof a === 'string' && typeof b === 'string') {
        return collator.compare(b, a)
      }
    
      return b - a;
    }
    
    function sortDesc(a, b) {
      if (typeof a === 'string' && typeof b === 'string') {
        return collator.compare(a, b);
      }
    
      return a - b;
    }
    
    0 讨论(0)
  • 2020-11-22 12:16

    also for both asec and desc sort, u can use this : suppose we have a variable SortType that specify ascending sort or descending sort you want:

     users.sort(function(a,b){
                return   sortType==="asc"? a.firstName.localeCompare( b.firstName): -( a.firstName.localeCompare(  b.firstName));
            })
    
    0 讨论(0)
  • 2020-11-22 12:17

    Suppose you have an array users. You may use users.sort and pass a function that takes two arguments and compare them (comparator)

    It should return

    • something negative if first argument is less than second (should be placed before the second in resulting array)
    • something positive if first argument is greater (should be placed after second one)
    • 0 if those two elements are equal.

    In our case if two elements are a and b we want to compare a.firstname and b.firstname

    Example:

    users.sort(function(a, b){
        if(a.firstname < b.firstname) { return -1; }
        if(a.firstname > b.firstname) { return 1; }
        return 0;
    })
    

    This code is going to work with any type.

    Note that in "real life"™ you often want to ignore case, correctly sort diacritics, weird symbols like ß, etc when you compare strings, so you may want to use localeCompare. See other answers for clarity.

    0 讨论(0)
  • 2020-11-22 12:17

    A generalized function can be written like below

        function getSortedData(data, prop, isAsc) {
            return data.sort((a, b) => (a[prop] < b[prop] ? -1 : 1) * (isAsc ? 1 : -1));
       }
    

    you can pass the below parameters

    1. The data which you want to sort
    2. The property in the data by it should be sorted
    3. The last parameter is of boolean type. It checks if you want to sort by ascending or by descending
    0 讨论(0)
提交回复
热议问题