ganesh kaspate
ganesh kaspate

Reputation: 2685

Sort array of objects having the offsets in javascript

I am new to web development. Here, I do have an array which is like

[{
        endOffset: "50"
        startOffset: "25"
        type: "Education"
    },
    {
        endOffset: "67"
        startOffset: "55"
        type: "address"
    },
    {
        endOffset: "65"
        startOffset: "59"
        type: "project"
    },
    {
        endOffset: "80"
        startOffset: "70"
        type:"fullname"
    }]

In this array of objects , now I am sorting it on the basis of the endoffsets . Which is like ->

jsonDataArray.sort(function (a, b) {
            return a.startOffset - b.startOffset;
          });

Then Its giving me the sorted array. But here it is sorting on the basis of only startoffset.

What I want is that I want to have an array like which will have sort on ascending order of both start and endoffset, so that the array will be like

[{
 starOffset: "25",
 type: "Education"
}, {
 endOffset: "50"
 type: "Education" },

{
 startoffset: "55"
 type: "address" },
{
 startoffset: "59"
 type: "project" },
{
 endoffset: "65"
 type: "project" },

{
 endoffset: "67"
 type: "address" 
},
{
 endoffset: "67"
 type: "address" 
},
{
 startOffset: "70"
 type: "address" 
},
{
 endoffset: "80"
 type: "address" 
},

]

So, How can I do this ? Can any one please help me with this ?

Upvotes: 2

Views: 683

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386560

You could create a new array with start and end values and sort them by taking either the start or end value.

var data = [{ endOffset: "50", startOffset: "25", type: "Education" }, { endOffset: "67", startOffset: "55", type: "address" }, { endOffset: "65", startOffset: "59", type: "project" }, { endOffset: "80", startOffset: "70", type: "fullname" }],
    extended = data.reduce(
        (r, { endOffset, startOffset, type }) =>
            r.concat({ startOffset, type }, { endOffset, type }), []);

extended.sort((a, b) => (a.startOffset || a.endOffset) - (b.startOffset || b.endOffset));

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

ES5

var data = [{ endOffset: "50", startOffset: "25", type: "Education" }, { endOffset: "67", startOffset: "55", type: "address" }, { endOffset: "65", startOffset: "59", type: "project" }, { endOffset: "80", startOffset: "70", type: "fullname" }],
    extended = data.reduce(function (r, o) {
        return r.concat(
            { startOffset: o.startOffset, type: o.type },
            { endOffset: o.endOffset, type: o.type }
        );
    }, []);

extended.sort(function (a, b) {
    return (a.startOffset || a.endOffset) - (b.startOffset || b.endOffset);
});

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

Upvotes: 1

HMR
HMR

Reputation: 39250

The following sorts on startOffset first then by endOffset:

const array = [{
  endOffset: "50",
  startOffset: "25",
  type: "Education"
},
{
  endOffset: "67",
  startOffset: "55",
  type: "address"
},
{
  endOffset: "65",
  startOffset: "59",
  type: "project"
},
{
  endOffset: "80",
  startOffset: "70",
  type:"fullname"
}]

console.log(
  array.reduce(
    (result,item)=>result.concat([
      {startOffset:item.startOffset,type:item.type},
      {endOffset:item.endOffset,type:item.type}
    ]),
    []
  ).sort(
    (a,b)=>(a.startOffset||a.endOffset)-(b.startOffset||b.endOffset)
  )
)

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

First create the desired array with startOffset and endOffset in separate object then you can assign the value for aVal and bVal to sort for by having condition in your sort function as mentioned below in code:

var jsonDataArray = [{
        endOffset: "50",
        startOffset: "25",
        type: "Education"
    },
    {
        endOffset: "67",
        startOffset: "65",
        type: "address"
    },
    {
        endOffset: "65",
        startOffset: "59",
        type: "project"
    },
    {
        endOffset: "80",
        startOffset: "70",
        type:"fullname"
    }];

var newJSON = [];
jsonDataArray.forEach((obj)=>{
  var  tempObj = {
     endOffset: obj.endOffset,
     type: obj.type
  };
  newJSON.push(tempObj);
  tempObj = {
     startOffset: obj.startOffset,
     type: obj.type
  };
  newJSON.push(tempObj);
});
newJSON.sort(function (a, b) {
  var aVal = a.startOffset?a.startOffset:a.endOffset;
  var bVal = b.startOffset?b.startOffset:b.endOffset;
  return aVal - bVal;
});
console.log(newJSON);

Upvotes: 1

Related Questions