Javascript sort an array except one

  • A+
Category:Languages

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

My array is:

    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"},] 

I am sorting this array using this code:

var Comparison_sort = this.Comparison.sort(function (a, b) {   if (a.key < b.key)       return -1;   if (a.key > b.key)       return 1;   return 0; }); 

This is sorting my array perfectly but I want one of my element to be on top means My element None should be on top and and sort all other elements.

For example I am getting this result:

   {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}    {key: "Cropping Intensity", value: "Cropping_Intensity"}    {key: "Culturable Waste", value: "Culturable_Waste"}     ....    {key: "None", value: "None"} 

But I want a result like this:

   {key: "None", value: "None"}    {key: "Barren Unculturable Land", value: "Barren_Unculturable_Land"}    {key: "Cropping Intensity", value: "Cropping_Intensity"}    {key: "Culturable Waste", value: "Culturable_Waste"}     .... 

I saw this answer Sort array in typescript but I was unable to use this answer to my problem.

 


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

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"}];  let 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);

// solution with explenation including helpers // left out in the above example  // creating helpers  // alias filter as select Array.prototype.select = Array.prototype.filter;  // create reject method that returns all elements // that return a falsey value if fn is evaluated // (opposite of filter/select) Array.prototype.reject = function (...args) {   let fn = args.shift();   return this.select((...sArgs) => !fn(...sArgs), ...args); }  // solution  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"}];  // function for select/reject to filter out 'None' elements let none = pair => pair.key === 'None';  // fetch all elements with the key 'None' let nones = comparison.select(none); // fetch all elements with the key not 'None' and sort them let others = comparison.reject(none).sort((a, b) => a.key.localeCompare(b.key)); // concatenate the 2 arrays together let result = nones.concat(others);  console.log(result);

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: