Sort an array except one element in JavaScript

前端 未结 10 2526
感动是毒
感动是毒 2021-02-12 01:31

I have an array and I am sorting it but I need to sort everything except one element of my array.

My array is:

var Comparison = [
    {key: \"None\", val         


        
相关标签:
10条回答
  • 2021-02-12 02:10
    const array1 = this.Comparison.filter(e => e.key === 'None');
    const array2 = this.Comparison
                .filter(e => e.key !== 'None')
                .sort((a, b) => a.key.localeCompare(b.key));
    
    this.Comparison = [].concat(array1, array2);
    
    0 讨论(0)
  • 2021-02-12 02:11

    Alternatively you can filter out the nones and sort the other elements. Then concatenate them back to each other at the end.

    const comparison = [{key: "None", value: "None"}, {key: "Geographical Area", value: "Geographical_Area"}, {key: "Forests", value: "Forests"}, {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}, {key: "Land put to Non agricultural use", value: "Land_put_to_Non_agricultural_use"}, {key: "Land Area", value: "Land_Area"}, {key: "Water Area", value: "Water_Area"}, {key: "Culturable Waste", value: "Culturable_Waste"}, {key: "Permanent Pastures", value: "Permanent_Pastures"}, {key: "Land under Tree Crops", value: "Land_under_Tree_Crops"}, {key: "Fallow Land excl Current Fallow", value: "Fallow_Land_excl_Current_Fallow"}, {key: "Current Fallow", value: "Current_Fallow"}, {key: "Total Unculturable Land", value: "Total_Unculturable_Land"}, {key: "Net Sown Area", value: "Net_Sown_Area"}, {key: "Gross Sown Area", value: "Gross_Sown_Area"}, {key: "Cropping Intensity", value: "Cropping_Intensity"}];
    
    const result = comparison
                   .filter(e => e.key === 'None')
                   .concat(
                     comparison.filter(e => e.key !== 'None')
                               .sort((a, b) => a.key.localeCompare(b.key))
                   );
                   
    console.log(result);

    Explanation:

    const comparison = [{key: "None", value: "None"}, {key: "Geographical Area", value: "Geographical_Area"}, {key: "Forests", value: "Forests"}, {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}, {key: "Land put to Non agricultural use", value: "Land_put_to_Non_agricultural_use"}, {key: "Land Area", value: "Land_Area"}, {key: "Water Area", value: "Water_Area"}, {key: "Culturable Waste", value: "Culturable_Waste"}, {key: "Permanent Pastures", value: "Permanent_Pastures"}, {key: "Land under Tree Crops", value: "Land_under_Tree_Crops"}, {key: "Fallow Land excl Current Fallow", value: "Fallow_Land_excl_Current_Fallow"}, {key: "Current Fallow", value: "Current_Fallow"}, {key: "Total Unculturable Land", value: "Total_Unculturable_Land"}, {key: "Net Sown Area", value: "Net_Sown_Area"}, {key: "Gross Sown Area", value: "Gross_Sown_Area"}, {key: "Cropping Intensity", value: "Cropping_Intensity"}];
    
    // fetch all elements with the key 'None'
    const nones = comparison.filter(e => e.key === 'None');
    // fetch all elements with the key not 'None'
    const others = comparison.filter(e => e.key !== 'None')
    // sort the elements in the array by key
                             .sort((a, b) => a.key.localeCompare(b.key));
    // concatenate the 2 arrays together
    const result = nones.concat(others);
    
    console.log(result);

    A bit of credit to Pac0s answer. After writing my solution I saw that I basically made a working version of his explanation. I'm too late to add my example to his answer because this is currently the most upvoted of the two.


    For larger arrays using filter() two times on the same array with the opposite predicate (check callback) might be a bit inefficient. You could opt to introduce a helper function like partition() to help reduce the amount of iteration that has to be done.

    function partition(forEachable, callback) {
      const partitions = { "true": [], "false": [] };
      forEachable.forEach((...args) => partitions[!!callback(...args)].push(args[0]));
      return [partitions[true], partitions[false]];
    }
    
    let comparison = [{key: "None", value: "None"}, {key: "Geographical Area", value: "Geographical_Area"}, {key: "Forests", value: "Forests"}, {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}, {key: "Land put to Non agricultural use", value: "Land_put_to_Non_agricultural_use"}, {key: "Land Area", value: "Land_Area"}, {key: "Water Area", value: "Water_Area"}, {key: "Culturable Waste", value: "Culturable_Waste"}, {key: "Permanent Pastures", value: "Permanent_Pastures"}, {key: "Land under Tree Crops", value: "Land_under_Tree_Crops"}, {key: "Fallow Land excl Current Fallow", value: "Fallow_Land_excl_Current_Fallow"}, {key: "Current Fallow", value: "Current_Fallow"}, {key: "Total Unculturable Land", value: "Total_Unculturable_Land"}, {key: "Net Sown Area", value: "Net_Sown_Area"}, {key: "Gross Sown Area", value: "Gross_Sown_Area"}, {key: "Cropping Intensity", value: "Cropping_Intensity"}];
    
    const [nones, others] = partition(comparison, e => e.key === "None");
    others.sort((a, b) => a.key.localeCompare(b.key));
    const result = nones.concat(others);
    console.log(result);

    0 讨论(0)
  • 2021-02-12 02:16

    A simple one-liner: if any of the keys in Array.prototype.sort compare function is 'None', then always put it on top, otherwise do basic comparison of keys with String.prototype.localeCompare():

    var comparison = [{key: "None", value: "None"}, {key: "Geographical Area", value: "Geographical_Area"}, {key: "Forests", value: "Forests"}, {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}, {key: "Land put to Non agricultural use", value: "Land_put_to_Non_agricultural_use"}, {key: "Land Area", value: "Land_Area"}, {key: "Water Area", value: "Water_Area"}, {key: "Culturable Waste", value: "Culturable_Waste"}, {key: "Permanent Pastures", value: "Permanent_Pastures"}, {key: "Land under Tree Crops", value: "Land_under_Tree_Crops"}, {key: "Fallow Land excl Current Fallow", value: "Fallow_Land_excl_Current_Fallow"}, {key: "Current Fallow", value: "Current_Fallow"}, {key: "Total Unculturable Land", value: "Total_Unculturable_Land"}, {key: "Net Sown Area", value: "Net_Sown_Area"}, {key: "Gross Sown Area", value: "Gross_Sown_Area"}, {key: "Cropping Intensity", value: "Cropping_Intensity"}];
    
    var sorted = comparison.sort((a,b) => a.key === 'None' ? -1 : b.key === 'None' ? 1 : a.key.localeCompare(b.key));
    
    console.log(sorted);

    0 讨论(0)
  • 2021-02-12 02:16

    If you want it positioned at the top you could just return -1 or you could return 1 if you want your item at the bottom of the list.

    You do not need multiple if's for the firstly or lastly positioned item which you want to exclude from the general sort.

    this.Comparison.sort(function (a, b) {
      if(a.key == b.key) return 0;
      if (a.key == 'None' || b.key == 'None') return -1;
    
      if (a.key < b.key)
          return -1;
      if (a.key > b.key)
          return 1;
      return 0;
    });
    
    0 讨论(0)
提交回复
热议问题