0

I have a scenario in which I need to display the data in an increasing order of the index number.

myArray = [
{custom_carousel: false, default_label: "SmartCards", index: 3, visible: true}, 
{custom_carousel: false, default_label: "Pathways", index: 2, visible: false},
{custom_carousel: false, default_label: "Pathways", index: 1, visible: false},
{custom_carousel: false, default_label: "Pathways", index: 0, visible: false}
]

Should I first sort the array or add if else if condition ?

if(index === 0){

}else if (index === 1){

}else if (index === 2){

}else if (index === 3){

}

7
  • 4
    .sort obviously. What if you have more items in your array? Are you gonna go and change the code and add more ifs every time that happens? "2 or more times? Do a loop." Commented Jan 31, 2019 at 12:41
  • 2
    Just use myArray.sort((a,b) => a.index - b.index) and loop through them for whatever you need them for. Commented Jan 31, 2019 at 12:42
  • 1
    Possible duplicate of Sorting an array of JavaScript objects and Sort Array based on Object Attribute - Javascript Commented Jan 31, 2019 at 12:47
  • I've commented above but reading this again I'm a bit confused. How exactly would you use the if/else to sort? Commented Jan 31, 2019 at 12:49
  • @adiga this is not a duplicate question. I need to know which one should be preferred. Commented Jan 31, 2019 at 12:50

4 Answers 4

2

You can use the sort method based on index values:

myArray = [
{custom_carousel: false, default_label: "Pathways", index: 2, visible: false},
{custom_carousel: false, default_label: "SmartCards", index: 3, visible: true}, 
{custom_carousel: false, default_label: "Pathways", index: 1, visible: false},
{custom_carousel: false, default_label: "Pathways", index: 0, visible: false}
]
console.log(myArray.sort((a,b)=>a.index - b.index));

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

Comments

2
myArray.sort(function(a, b){return a.index - b.index});

Will sort your array by index

Comments

1

Sorting will take O(n+log(n)) time and if-else-if-else will take O(n2) time. So sorting is better choice.

2 Comments

I'm not -1 but why is complexity even a question here?
Author asked which one they must choose. I give one of reasons.
1

I suggest to sort the array in advance and then access by the index of the array.

Reasons

  • easy to maintain with more elements of the array
  • easy to access in wanted order
  • code ie easy to understand, instead of having some code, which reason is not obvious and requieres explanation
  • access is fast, instead of iterating the array always from the beginning for getting the next item.

var myArray = [{ custom_carousel: false, default_label: "SmartCards", index: 3, visible: true }, { custom_carousel: false, default_label: "Pathways", index: 2, visible: false }, { custom_carousel: false, default_label: "Pathways", index: 1, visible: false }, { custom_carousel: false, default_label: "Pathways", index: 0, visible: false }]

myArray.sort(({ index: a }, { index: b }) => a - b);

console.log(myArray);

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.