2

I have array which has sequence number. I want to pick top 3 sequence numbers from array based not sequence number only like "sequesnce": "9.80.709.5 This is my array

array1=[
{
    "orgId": 101,
    "dId": 494,
    "name": "Test1",
    "sequesnce": "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR"
},
{
    "orgId": 102,
    "dId": 442,
    "name": "Test2",
    "sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",
},
{
    "orgId": 103,
    "sequesnce": "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR",
    "dId": 494,
    "name": "Test3",
},
{
    "orgId": 103,
    "sequesnce": "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR",
    "dId": 494,
    "name": "Test3",
},
{
    "orgId": 103,
    "sequesnce": "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR",
    "dId": 494,
    "name": "Test3",
},

I am looking for output

topThree=
{
"sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",
},
{
"sequesnce": "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR",
},
{
"sequesnce": "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR",
},

I tried this

function arrayMax(arr) {
return arr.reduce(function (p, v) {
  return ( p > v ? p : v );
});
}]

var maximo = arrayMax(obj); //return the high

This returns me

{
    "orgId": 102,
    "dId": 442,
    "name": "Test2",
    "sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",
},

This code return single array not three and not sure in all cases this logic going to work.

7
  • 2
    Why is 9.80.709.5 the maximum and not 9.408.90.3? Commented Oct 15, 2020 at 7:49
  • @Nick - 9.408.90.3 is old release than 9.80.709.5 Commented Oct 15, 2020 at 7:51
  • 2
    I guess I don't understand your numbering system... I would have thought 9.408.xxx was newer than 9.80.xxx Commented Oct 15, 2020 at 7:52
  • @Nick - The way you said its correct. I was confused. Commented Oct 15, 2020 at 7:58
  • Will sequence ALWAYS be 1.1.1.1 to 999.999.999.999? Or could there be a 999.999 Commented Oct 15, 2020 at 8:32

3 Answers 3

5

You could sort with String#localeCompare and take an options to sort by columns of dotted values.

At the end take the top three items with Array#slice.

const
    array = [{ orgId: 101, dId: 494, name: "Test1", sequesnce: "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR" }, { orgId: 102, dId: 442, name: "Test2", sequesnce: "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR" }, { orgId: 103, sequesnce: "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR", dId: 494, name: "Test3" }, { orgId: 103, sequesnce: "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR", dId: 494, name: "Test3" }, { orgId: 103, sequesnce: "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR", dId: 494, name: "Test3" }],
    top3 = array
        .sort((a, b) => b.sequesnce.localeCompare(a.sequesnce, undefined, { numeric: true, sensitivity: 'base' }))
        .slice(0, 3)

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

Sign up to request clarification or add additional context in comments.

2 Comments

Great solution as long as the sequence is 1.1.1.1 to 999.999.999.999 and not suddenly 999.999
@R15, you need to take an object, maybe take Object.values(yourObject) and sort it with the above.
2

You need to expand the tuples before sorting if you have shorter sequences with higher leading values - that will not work with localeCompare

const maxLen = 4;
const expandTuple = tpl => {
  tpl = tpl.split('.');
  while (tpl.length < maxLen) tpl.unshift("000");
  return tpl.map(x => x.padStart(3, "0")).join('.');
};
const array1 = [{ "orgId": 101, "dId": 494, "name": "Test1",
    "sequence": "6.408.06.0 Sequence Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR" },{"orgId": 102,"dId": 442,"name": "Test2",
    "sequence": "9.80.709.5 Sequence Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",},{"orgId": 103,
    "sequence": "99.1.1 NOTE THIS IS SHORTER Sequence Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR","dId": 494,"name": "Test3",},{"orgId": 103,
    "sequence": "8.208.409.0 Sequence Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR","dId": 494,"name": "Test3",},{"orgId": 103,
    "sequence": "9.408.90.3 Sequence Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR","dId": 494,   "name": "Test3",  }
]


const three = array1.sort((a,b) => {
  const aTub = a.sequence.split(" ")[0];
  const bTub = b.sequence.split(" ")[0];
  if (expandTuple(aTub)<expandTuple(bTub)) return 1
  if (expandTuple(aTub)>expandTuple(bTub)) return -1
  return 0;
}).slice(0,3)
console.log(three)

// ---- compare other solution which fails on the shorter sequence

console.log(array1
  .sort((a, b) => b.sequence.localeCompare(a.sequence, undefined, { numeric: true, sensitivity: 'base' }))
  .slice(0, 3)
)

Comments

1
  • First, using Array.map, you can generate the array of sequesne item objects only.
  • And using Array.sort, you can sort the array by descending order.
  • And using Array.splice, you can extract the top 3 items from the sorted array.

const array1 = [{
    "orgId": 101,
    "dId": 494,
    "name": "Test1",
    "sequesnce": "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR"
  },
  {
    "orgId": 102,
    "dId": 442,
    "name": "Test2",
    "sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",
  },
  {
    "orgId": 103,
    "sequesnce": "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR",
    "dId": 494,
    "name": "Test3",
  },
  {
    "orgId": 103,
    "sequesnce": "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR",
    "dId": 494,
    "name": "Test3",
  },
  {
    "orgId": 103,
    "sequesnce": "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR",
    "dId": 494,
    "name": "Test3",
  }
];

const sortedArr = array1.map(({ sequesnce }) => ({ sequesnce })).sort((a, b) => (b.sequesnce.localeCompare(a.sequesnce, undefined, { numeric: true, sensitivity: 'base'})));

const topCount = 3;
console.log(sortedArr.splice(0, topCount));

1 Comment

This does not produce the correct results. See OPs comments to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.