-1

firstArray

 [
 {
  type:"one",
  ID : "1",
  isValid:"true"
 },
 {
  type:"two",
  ID : "2",
  isValid:"false"
 },
 {
  type:"two",
  ID : "3",
  isValid:"true"
 }
]

I have an array of object like the above one. i have to take all the valid objects. By iterating them,I ll get an array of objects which contains only valid objects. I have done this part successfully.

 var valid_types = $.grep(firstArray, function(v)
  { 
         return v.isValid=== "true";
  });

 ["two","five","three","one","four"]

I have another array which specifies the sorting order. How do i sort the resulting array based on the type? I am confused a bit. Any suggestions? I have to sort valid_types based on type

Doubts: Resulting array can have same type of valid objects and it may not have all the types specified in the sorting order. How to handle this?

DESIRED ARRAY:

[
  {
  type:"two",
  ID : "3",
  isValid:"true"
  },
  {
  type:"one",
  ID : "1",
  isValid:"true"
  }
]
Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • Please tell me the reason for downvoting – Gibbs Oct 31 '14 at 13:11
  • 3
    I didn't downvote, but I suspect that the person who did (who should have explained why in a comment) was concerned about the lack of code in your question. – Pointy Oct 31 '14 at 13:14
  • @Pointy thanks. I don't know how to sort it shortly. That's why i posted here. I have done half of my part. What code are you expecting still? – Gibbs Oct 31 '14 at 13:15
  • Or maybe because your object is not valid – Andreas Furster Oct 31 '14 at 13:15
  • What is the desired resulting array? – hindmost Oct 31 '14 at 13:15
  • Desired resulting array should be in the order which is based on the second array i provided. Sort is not based on ID. It's based on String 'type' – Gibbs Oct 31 '14 at 13:16
  • You can use `.filter()` to create an array with valid objects, and then you just need to call `.sort()` on the result. – Pointy Oct 31 '14 at 13:16

2 Answers2

1

I made a solution, but it's not really good to do it like this.

First i add a property to each object with the int value of the type. After that you can sort them by that property.

var arr = [
 {
  type:"two",
  ID : "2",
  isValid:"false"
 },
 {
  type:"one",
  ID : "1",
  isValid:"true"
 },
 {
  type:"two",
  ID : "3",
  isValid:"true"
 }
];

for(var i = 0; i < arr.length; i++){
  switch(arr[i].type){
    case "one":
      arr[i].typeNum = 1;
      break;
    case "two":
      arr[i].typeNum = 2;
      break;
  }
}

arr.sort(function(a, b){
 return a.typeNum-b.typeNum
});

console.log(arr);
Andreas Furster
  • 1,558
  • 1
  • 12
  • 28
1

You can use the .sort() function to sort your array with a custom function.

var order =  ["two","five","three","one","four"];
var data = [{
        type:"one",
        ID : "1",
        isValid:"true"
    },
    {
        type:"two",
        ID : "2",
        isValid:"false"
    },
    {
        type:"two",
        ID : "3",
        isValid:"true"
    }
];

data.sort(function(a, b) {
    return order.indexOf(a.type) - order.indexOf(b.type);
});
//use your sorted array
Benjamin P.
  • 453
  • 5
  • 12