Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort an array except one element in JavaScript

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", 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 elements to be on top, meaning my element None should be on top 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 an answer, Sort array in TypeScript, but I was unable to use this answer to my problem.

like image 648
Nilay Singh Avatar asked Jul 27 '18 11:07

Nilay Singh


2 Answers

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

tells "do a regular sort, except if the key is none which means it must go first."

like image 98
spi Avatar answered Sep 18 '22 07:09

spi


Not fancy, but a pretty straightforward way of doing this is to just remove the special element, sort the array, and insert the special to whatever index you want.

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" },];    const idx = Comparison.findIndex(a => a.key === 'None');  const none = Comparison.splice(idx, 1);  Comparison.sort((a, b) => a.key.localeCompare(b.key));  Comparison.splice(0,0, none[0]);    console.log(Comparison);

To avoid no special or multiple special element issues:

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" },];    const obj = Comparison.reduce((acc, a) => {    if (a.key === 'None') {      acc.f.push(a);    } else {      const idx = acc.s.findIndex(b => b.key.localeCompare(a.key) > 0);      acc.s.splice(idx === -1 ? acc.s.length : idx, 0, a);    }    return acc;  }, { f: [], s: [] });    const res = obj.f.concat(obj.s);    console.log(res);
like image 23
Hikmat G. Avatar answered Sep 19 '22 07:09

Hikmat G.