-4

I have to sort an array of objects based on 'x' value.

Object Name: Item Item has an int value "x"

for eg:

Array = {
  "Item", //x value 3 
  "Item", //x value 2 
  "Item", //x value 3 
  "Item", //x value 4
}

in the above array index value of object at index 0 and 2 are same. so preserve the order of Item value 3.

After sorting the array will return the value like this

 Array = {
      "A", //x value 2
      "A", //x value 3 // This object was at index 0 before sorting
      "A", //x value 3 // This object was at index 2 before sorting
      "A", //x value 4
    }

The above order is important.

how can sort it.

iPhone Guy
  • 1,285
  • 3
  • 23
  • 34
  • 1
    Your array doesn't contains plain string, correct? Are you using a custom object? In that case you should check this question http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it – mxb Jun 30 '14 at 10:00
  • Would it be possible to provide the array initialization code in Objective-C? Or do you also want to know how to create an array that supports that kind of sorting? – Markus Rautopuro Jun 30 '14 at 10:04
  • I editted my question. please check it. if you are not clear. comment me. – iPhone Guy Jun 30 '14 at 10:20

1 Answers1

0

I didn't understand what do you exactly mean, but there is a cool method sortedArrayUsingComparator:

NSArray* sortedArray;
sortedArray = [originArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    // compare your objects as you wish!
}];
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51