1

I have a 2D array with multiples values.

One field on this array is called group, let's imagine my array have this order:

private var myArray = [
   ArrayModel(
      id: 0,
      name: "Test",
      color: 0,
      img: "img.png",
      group = "myGroup"),
   ArrayModel(
      id: 1,
      name: "Test1",
      color: 0,
      img: "img.png",
      group: "myGroup"),
   ArrayModel(
      id: 2,
      name: "Test2",
      color = 0,
      img = "img.png",
      group = "myGroup3")
   ArrayModel(
      id: 3,
      name: "Test3",
      color: 0,
      img: "img.png",
      group: "myGroup2"),
   ArrayModel(
      id: 4
      name: "Test4"
      color: 0
      img: "img.png"
      group: "myGroup3")
]

Array Model

class ArrayModel: Decodable {

var id: Int
var name: String
var color: Int
var img: String
var group: String

convenience init() {
    self.init()
    id = 0
    name = ""
    color = 0
    img = ""
    group = ""
}

init(id: Int, name: String, color: Int, img: String, group: String) {
    self.id = id
    self.name = name
    self.color = color
    self.img = img
    self.group = group
}

}

How can I move my myArray[2] to be in myArray[1] ?

My desired output is :

let myArray = [
   ArrayModel(
      id: 0,
      name: "Test",
      color: 0,
      img: "img.png",
      group: "myGroup")
   ]
   [1] => same shema, but group => "myGroup"
   [2] => same shema, but group => "myGroup2"
   [3] => same shema, but group => "myGroup2"
   [4] => same shema, but group => "myGroup3"
]

I tried this :

myArray.sorted(by: { $0.group < $1.group })

Thanks for your help!

Community
  • 1
  • 1

2 Answers2

1

This may help you to sorted by Case Sensitive but your myArray should be mutable. ex : var myArray

myArray = myArray.sorted(by: { $0.group.localizedCompare($1.group) == .orderedAscending})
Pranavan SP
  • 1,785
  • 1
  • 17
  • 33
1

Try this:

myArray.sort { $0.group < $1.group }

The .sort(by: ...) isn't what you need here, as you want to modify myArray (by sorting it). To do that, you have to omit the by:....

For more information, see this answer :)

Adi219
  • 4,712
  • 2
  • 20
  • 43
  • 1
    This doesn't make sense `sorted` return sorted array its not mutable method – Nirav D Aug 08 '18 at 09:17
  • Check the apple doc https://developer.apple.com/documentation/swift/array/2296815-sorted it will return sorted array you need to store the result, sort is the mutating method https://developer.apple.com/documentation/swift/array/2296801-sort – Nirav D Aug 08 '18 at 09:18
  • @NiravD Please see https://stackoverflow.com/questions/24130026/swift-how-to-sort-array-of-custom-objects-by-property-value/24130058#24130058 – Adi219 Aug 08 '18 at 09:19
  • 2
    If you check the answer it is using `sort` method not the `sorted` check apple doc – Nirav D Aug 08 '18 at 09:21
  • @NiravD Yep, you're right! What you've been saying makes sense. Edited my answer :) – Adi219 Aug 08 '18 at 09:22
  • No, sorted was right, with `sort` I have an error `Value of tuple type '()' has no member 'subscript'` – Romain Sickenberg Aug 08 '18 at 09:31