I have a list of string:
var nameList = ["Apple", "Watermelon", "orange", ...]
The list is returned from backend, order not garanteed.
I only want to have orange
to always be the 1st element in array, no need to care the other elements' order.
I try to use nameList.sort { $0 // what to do}
, but get stuck, because I only want one element to be the first element.
How to achieve it?
====== UPDATE ======
A followup question. If I have a list of fruit objects, each fruit
is a custom struct
object:
struct Fruit {
public let name;
public let weight;
init(_ name: String, _ weight: Double) {
self.name = name
self.weight = weight
}
}
Now, I got a list of Fruit
:
var fruitList:[Fruit] = getDataFromBackend()
I would like to have the fruit
with name
"orange" always be the first item, how to do now?