I have a list that I need to sort by "popularity" (more popular to less popular), this value comes from a JSON API. Is there a Sort function by given value in SwiftUI?
The JSON looks like this:
[
{
"id": 1,
"nombre": "The design of every day things",
"autor": "Don Norman",
"disponibilidad": true,
"popularidad": 70,
"imagen": "https://images-na.ssl-images-amazon.com/images/I/410RTQezHYL._SX326_BO1,204,203,200_.jpg"
},
{
"id": 2,
"nombre": "100 años de soledad",
"autor": "Garcia Marquez",
"disponibilidad": false,
"popularidad": 43,
"imagen": "https://images-na.ssl-images-amazon.com/images/I/51egIZUl88L._SX336_BO1,204,203,200_.jpg"
}
]
There's more to it, that's just a piece of it.
Popularity is popularidad.
This is currently my list:
var body: some View {
NavigationView {
ScrollView {
VStack(alignment: .leading) {
ForEach(booksVM.books) { book in
HStack {
Text(book.nombre)
Spacer()
}
Text(book.autor)
Spacer()
}
}.padding(.leading, 5)
}
.navigationBarTitle("Bienvenido a la librería flux")
.onAppear(perform: self.booksVM.fetchBooks)
}
}
Please let me know if more code is needed to understand this.