skip to Main Content

I have array of object in which i need to sort based on condition using javascript.

I need to sort the others list based on tariffId but in the order 7201, 7202, 7203, 7205, 7204

means always 7204 is the last.rest can be in asc order

var arrobj = [
  {id:1, others: [ {place: "IN", tariffId:7201}, {place: "IN", tariffId:7204}, {place: "IN", tariffId:7203} ] }
  {id:1, others: [ {place: "IN", tariffId:7201}, {place: "IN", tariffId:7204}, {place: "IN", tariffId:7205} ] }
  {id:1, others: [ {place: "IN", tariffId:7202}, {place: "IN", tariffId:7204}, {place: "IN", tariffId:7201} ] }
]

Expected output

 {id:1, others: [ {place: "IN", tariffId:7201}, {place: "IN", tariffId:7203}, {place: "IN", tariffId:7204} ] }
  {id:1, others: [ {place: "IN", tariffId:7201}, {place: "IN", tariffId:7205}, {place: "IN", tariffId:7204} ] }
  {id:1, others: [ {place: "IN", tariffId:7201}, {place: "IN", tariffId:7202}, {place: "IN", tariffId:7204} ] }
]

Tried

var result = arrobj.map(e =>
 e.others.sort((a,b)=> a-b).map(i => i))
;

2

Answers


  1. You had it almost already, just need to move 7204 to the end of the new sorted array.

    const IN = "IN"; //needed for running this example
    
    let arrobj = [
      {id:1, others: [ {place: IN, tariffId:7201}, {place: IN, tariffId:7204}, {place: IN, tariffId:7203} ] },
      {id:1, others: [ {place: IN, tariffId:7201}, {place: IN, tariffId:7204}, {place: IN, tariffId:7205} ] },
      {id:1, others: [ {place: IN, tariffId:7202}, {place: IN, tariffId:7204}, {place: IN, tariffId:7201} ] }
    ]
    
    arrobj.map(elem =>
      elem.others.sort((a, b) => {
        if (a.tariffId === 7204) return 1; // Move 'a' to the end
        if (b.tariffId === 7204) return -1; // Move 'b' to the end
        return a.tariffId - b.tariffId; // Sort the rest normally
      })
    )
    
    console.log(arrobj);
    Login or Signup to reply.
  2. There are multiple ways of doing this, but here a simple solution might be to transform 7204 into a large number during sort.

    eg.

    var arrobj = [
        { id: 1, others: [{ place: "IN", tariffId: 7201 }, { place: "IN", tariffId: 7204 }, { place: "IN", tariffId: 7203 }] },
        { id: 1, others: [{ place: "IN", tariffId: 7201 }, { place: "IN", tariffId: 7204 }, { place: "IN", tariffId: 7205 }] },
        { id: 1, others: [{ place: "IN", tariffId: 7202 }, { place: "IN", tariffId: 7204 }, { place: "IN", tariffId: 7201 }] }
    ]
    
    const last7204 = v => v === 7204 ? 10000000 : v;
    
    var result = arrobj.map(e => {
        e.others = e.others.sort((a, b) => last7204(a.tariffId) - last7204(b.tariffId))
        return e
    })
    
    console.log(result)

    A little side note: your using map here, but you might not realise your still mutating the original array. If you do console.log(arrobj) you will see what I mean. A small fix for this would be to do ->

    var result = arrobj.map(e => {
        return [...e.others].sort((a, b) => last7204(a.tariffId) - last7204(b.tariffId))
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search