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!

19
  • 3
    Please can you show your exact input and desired output? (as well as any code you've tried?) Commented Aug 8, 2018 at 8:49
  • 1
    It's not clear what you're asking - a (deleted) answer correctly showed how to move myArray[2] to myArray[1] Commented Aug 8, 2018 at 8:51
  • 1
    It is edited now. Sorry for the wrong formulation, it's a bit hard to explain myself on problems like this :-) Commented Aug 8, 2018 at 8:53
  • 2
    You say it's a 2D array but it looks more like an array of dictionaries or structs. Could you show us some real code instead of this pseudo code? Commented Aug 8, 2018 at 8:58
  • 1
    I've edited your question to show my understanding of what myArray initially looks like. Am I correct? (If not, feel free to roll back the edit) Commented Aug 8, 2018 at 8:59

2 Answers 2

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 :)

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

6 Comments

This doesn't make sense sorted return sorted array its not mutable method
Check the apple doc developer.apple.com/documentation/swift/array/2296815-sorted it will return sorted array you need to store the result, sort is the mutating method developer.apple.com/documentation/swift/array/2296801-sort
If you check the answer it is using sort method not the sorted check apple doc
@NiravD Yep, you're right! What you've been saying makes sense. Edited my answer :)
|
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})

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.