0

I have the following data:

[["Balin Sankar","10","English","91.1408767700195"],
["Balin Sankar","10","Maths","88.1268997192383"],
["Balin Sankar","10","Science","90.3445739746094"],
["Balin Sankar","10","History","87.1235580444336"],
["Balin Sankar","10","Geography","88.2675628662109"],
["Balin Sankar","10","Civics","89.6238479614258"],
["Balin Sankar","10","Economics","86.5236434936523"]]

I need to sort these based on the 4th value

2 Answers 2

3

You could pass lodash's sortBy function a function that selects the fourth item in the list:

var result = _.sortBy(data, item => parseFloat(item[3]))

var data = [["Balin Sankar","10","English","91.1408767700195"],
["Balin Sankar","10","Maths","88.1268997192383"],
["Balin Sankar","10","Science","90.3445739746094"],
["Balin Sankar","10","History","87.1235580444336"],
["Balin Sankar","10","Geography","88.2675628662109"],
["Balin Sankar","10","Civics","89.6238479614258"],
["Balin Sankar","10","Economics","86.5236434936523"]]

var result = _.sortBy(data, item => parseFloat(item[3]))

var output = document.getElementById('result')

_.each(result, item => output.textContent += (item[2] + ' ' + item[3] + '\n'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.2/lodash.js"></script>

<p>
  <pre id="result"></pre>
</p>

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

Comments

1

You don't need lodash for that. It can be achieved with a regular sort:

var array = [["Balin Sankar","10","English","91.1408767700195"],
["Balin Sankar","10","Maths","88.1268997192383"],
["Balin Sankar","10","Science","90.3445739746094"],
["Balin Sankar","10","History","87.1235580444336"],
["Balin Sankar","10","Geography","88.2675628662109"],
["Balin Sankar","10","Civics","89.6238479614258"],
["Balin Sankar","10","Economics","86.5236434936523"]];

array.sort(function(a, b) {
  return a[3] - b[3];
});

console.log(array);

Comments

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.