0

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.

4
  • So, what do you get? Providing a full example that someone could copy-paste and start experimenting would greatly improve the chances for you to get an answer. Commented Jun 28, 2018 at 8:28
  • Think about the logic. if previous.selected == true, then it should "go up in the list". if previous.selected == false, then it should "go down", same for "next". Then, compare previous and next with that approach: if next.selected && previous.selected { return true } else if !next.selected && previous.selected { return true } else if next.selected && !previous.selected {return false } else {//both selected == false; return false} in a non optimized but more explicit algorithm. Note: I may have inverted the return true/return false Commented Jun 28, 2018 at 8:33
  • Mr Vadian, THIS QUESTION IS NOT DUPLICATED. READ WITH A LITTLE ATTENTION THE QUESTION, PLEASE. Commented Jun 28, 2018 at 9:21
  • it is indeed a duplicate... Read the duplicate carefully the answer resides in it. Commented Jun 28, 2018 at 11:39

2 Answers 2

0

try this:

let sortedTable = iTable.sorted(by: {$0.selected && !$1.selected})
Sign up to request clarification or add additional context in comments.

1 Comment

Is what I have done and but It does not order the contents of the internal table.
0

If you only care about the selected property, just write

let sortedTable = iTable.sorted { (previous, next) -> Bool in
    return previous.selected
}

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.