Mr Vadian, THIS QUESTION IS NOT DUPLICATED. READ WITH A LITTLE ATTENTION THE QUESTION, PLEASE!
I want group all occurrences with a boolean variable value "true" first, and then, group all occurrences with a boolean variable value "false".
struct myStruct {
var itemID: Date
var itemName: String
var selected: Bool
}
var iTable = [myStruct]()
I try to sort iTable by Boolean variable:
let sortedTable = iTable.sorted { (previous, next) -> Bool in
return next.selected && previous.selected
}
iTable = sortedTable
(I will improve the example to explain me better)
I use the contents of the internal table (struct) with a Table View
(itemID: 111, itemName: John, selected: true)
(itemID: 477, itemName: Rose, selected: false)
(itemID: 431, itemName: Peter, selected: true)
(itemID: 215, itemName: Mary, selected: true)
(itemID: 442, itemName: Marisa, selected: false)
After sorting (by Boolean variable "selected"), I list the contents of internal table "iTable" (only the names) and I get the same wrong output.
John
Rose
Peter
Mary
Marisa
...
If (repeat If) I sort by name, "itemName" variable (alphabetically)
iTable = iTable.sorted { $0.itemName < $1.itemName }
_tableView.reloadData()
The result is perfect!
In the same way that alphabetically, but sorting by Boolean variable I would like to obtain:
John (true)
Peter (true)
Mary (true)
Rose (false)
Marisa (false)
...
My shabby solution:
CREATE 2 INTERNAL TABLES, 1 WITH ALL THE OCCURRENCES WITH VALUE "true" AND ANOTHER WITH ALL THE OCCURRENCES WITH VALUE "false".
THEN, MERGE THE 2 INTERNAL TABLES OBTAINING THE DESIRED RESULT.