5

I have an array of objects that looks like this:

var data = [
 {
   title: 'Shirt',
   position: 3
 },
 {
   title: 'Ball',
   position: 1,
 }
]

How could I sort it for use in a for loop like this.

for(var i in data) {

}

I tried:

for(var i in data | orderBy:'position')

But that is angular so normal Javascript it doesn't work.

I'm thinking their must be some way to sort the array before looping through it, or adding a filter to the loop, not sure which is the best way.

Jordash
  • 2,926
  • 8
  • 38
  • 77
  • 2
    does this post answer your query? https://stackoverflow.com/questions/2466356/javascript-object-list-sorting-by-object-property – andriusain Jul 19 '18 at 01:06
  • 2
    Why do you want to use a `for in` loop? Why not `sort`, whose abstract sorting rules are much easier to implement than your own custom sorting function? – CertainPerformance Jul 19 '18 at 01:06

4 Answers4

21

But that is angular so normal Javascript it doesn't work.

Simply you can use JavaScript sort function. It will work in Angular(TypeScript) also.

Note: When sorting numbers, you can simply use the compact comparison:

myArray.sort((n1,n2) => n1 - n2);

var data = [
 {
   title: 'Shirt',
   position: 3
 },
 {
   title: 'Ball',
   position: 1,
 }
];

 data.sort(function(a, b) { 
return a.position- b.position;
})

console.log(data);
NullPointer
  • 7,094
  • 5
  • 27
  • 41
3

Use Array.prototype.sort (doc) and pass the compare function as you want:

var data = [
 {
   title: 'Shirt',
   position: 3
 },
 {
   title: 'Ball',
   position: 1,
 },
 // add for actually seeing the correct result
 {
   title: 'Cake',
   position: 2,
 }
];

function compareFunction(a,b){
  if(a.position > b.position)
    return 1;
  else
    return -1;
}

data.sort(compareFunction);

console.log(data);
Terry Wei
  • 1,521
  • 8
  • 16
0

You can use arr.sort([compareFunction]).

If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

If compareFunction(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first.

compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.

For Example -

     myarray.sort((a,b) => {
       if(a.position > b.position)
        return 1
        else
        return -1
})
himank
  • 439
  • 3
  • 5
0

Here is a bubble sort approach:

var data = [
    {
      title: 'Shirt',
      position: 3
    },
    {
      title: 'Ball',
      position: 1,
    },
    {title: "h",
position:0}
   ]
  

   for(let i=0;i<data.length;i++)
   for(let j=0;j<data.length - 1;j++){
    if(data[j].position > data[j + 1].position){
    var first = data[j]
    var second = data[j + 1]
    data[j] = second;
    data[j + 1] = first;
    
    }
    
    }


console.log(data)