Home > Mobile >  Sort JSON Array based on Key Value
Sort JSON Array based on Key Value

Time:05-26

I have an array of Objects with status 'Pass' & 'Fail'. I want to move all Fail one's at the top and Pass one's at Bottom. Is there any array method to do the same?

let a = [
  { name: 'x'  , status: 'Pass' },
  { name: 'x1' , status: 'Fail' },
  { name: 'x2' , status: 'Pass' },
  { name: 'x3' , status: 'Fail' }
];

Required Output is

a = [
  { name: 'x1' , status: 'Fail' },
  { name: 'x3' , status: 'Fail' },
  { name: 'x'  , status: 'Pass' },
  { name: 'x3' , status: 'Pass' }
];

CodePudding user response:

    let failed = []
    let passed = []
       for(let i = 0; i < values.length; i  ){ 
         if(values[i].pass){
            passed.push(values[i])
         else {
            failed.push(values[i])
          }
       }
values = []
values.push(...failed)
values.push(...passed)

CodePudding user response:

You can use Array.sort function to sort your array of objects. code :

let a = [
      {
        name: "x",
        status: "Pass",
      },
      {
        name: "x1",
        status: "Fail",
      },
      {
        name: "x2",
        status: "Pass",
      },
      {
        name: "x3",
        status: "Fail",
      },
    ];
    a.sort(function (a, b) {
      var keyA = a.status,
        keyB = b.status;
      // Compare the 2 values
      if (keyA < keyB) return -1;
      if (keyA > keyB) return 1;
      return 0;
    });
    console.log(a);

CodePudding user response:

An idiomatic solution for this task:

[...a.filter(el=>el.status==='Fail'),...a.filter(el=>el.status==='Pass')]

However, I thinq @farooq's solution is the best here

CodePudding user response:

Using Ascii Value you can sort array as well

a.sort((a, b) => {
  return a.status.charAt(0) > b.status.charAt(0)
    ? 1
    : a.status.charAt(0) < b.status.charAt(0)
    ? -1
    : 0;
});

CodePudding user response:

If you are just sorting lexicographically, simply use localeCompare, and logical-OR the status and name comparison values.

let data = [
  { name: 'x'  , status: 'Pass' },
  { name: 'x1' , status: 'Fail' },
  { name: 'x2' , status: 'Pass' },
  { name: 'x3' , status: 'Fail' }
];

const sorted = data.sort(
  ({ name: n1, status: s1 }, { name: n2, status: s2 }) =>
    s1.localeCompare(s2) || n1.localeCompare(n2))

console.log(sorted);
.as-console-wrapper { top: 0; max-height: 100% !important; }

CodePudding user response:

Here is one way you can do this with Array#sort and String#localeCompare methods.

const a = [ { name: 'x', status: 'Pass' }, { name: 'x1', status: 'Fail' }, { name: 'x2', status: 'Pass' }, { name: 'x3', status: 'Fail' } ],

      output = a.sort(({status:A},{status:B}) => A.localeCompare(B));
      
console.log( output );

CodePudding user response:

You can use Array.sort() combined with String.localeCompare() as follows:

let a = [
  { name: 'x'  , status: 'Pass' },
  { name: 'x1' , status: 'Fail' },
  { name: 'x2' , status: 'Pass' },
  { name: 'x3' , status: 'Fail' }
];

const result = a.sort((o1, o2) => o1.status.localeCompare(o2.status));

console.log(result)

CodePudding user response:

You could also use forEach

let failed = []
let passed = []
values = [
  { name: 'x'  , status: 'Pass' },
  { name: 'x1' , status: 'Fail' },
  { name: 'x2' , status: 'Pass' },
  { name: 'x3' , status: 'Fail' }
]

values.forEach((e) => {
  if (e.status === 'Pass') {
    passed.push(e)
    return
  }
  failed.push(e)
})

values = []
values.push(...failed)
values.push(...passed)

CodePudding user response:

I would like to use this way:

let a = [
      {
        name: "x",
        status: "Pass",
      },
      {
        name: "x1",
        status: "Fail",
      },
      {
        name: "x2",
        status: "Pass",
      },
      {
        name: "x3",
        status: "Fail",
      },
    ];

let b = [];
for(var x in a ) {
  if(a[x].status == 'Fail') { b.unshift(a[x]); }
  if(a[x].status == 'Pass') { b.push(a[x]); }
}
console.log(b);

  • Related