I have a nested set model in SQLite database. The siblings need to be sorted alphabetically. Below is the full set, but the siblings that need to be sorted is the second level. As you can see, they start in this order:
Kanval, Wafiyah, Qamar, Lamya, Chaman, Fadila
familyName | lft | rgt
----------------------+------+-----
Families | 1 | 62
-- Kanval | 2 | 9
---- Omera | 3 | 4
---- Dafiyah | 5 | 6
---- Daneen | 7 | 8
-- Qamar | 10 | 19
---- Deeba | 11 | 12
---- Pakeezah | 13 | 14
---- Rabiya | 15 | 16
---- Banafsha | 17 | 18
-- Lamya | 20 | 33
---- Banujah | 21 | 22
---- Buthaynah | 23 | 24
---- Vardah | 25 | 26
---- Kaneez | 27 | 28
---- Parveen | 29 | 30
---- Ghunyah | 31 | 32
-- Chaman | 34 | 45
---- Kanz | 35 | 36
---- Varisha | 37 | 38
---- Kunza | 39 | 40
---- Khusbakht | 41 | 42
---- Ermina | 43 | 44
-- Fadila | 46 | 53
---- Tahani | 47 | 48
---- Iffah | 49 | 50
---- Huwaydah | 51 | 52
-- Wafiyah | 54 | 61
---- Asheeyana | 55 | 56
---- Hutun | 57 | 58
---- Aakifah | 59 | 60
But the need to be sorted to this order:
Chaman, Fadila, Kanval, Lamya, Qamar, Wafiyah
familyName | lft | rgt
------------------+-----+-----
Families | 1 | 62
--Chaman | 2 | 13
----Kanz | 3 | 4
----Varisha | 5 | 6
----Kunza | 7 | 8
----Khusbakht | 9 | 10
----Ermina | 11 | 12
--Fadila | 14 | 21
----Tahani | 15 | 16
----Iffah | 17 | 18
----Huwaydah | 19 | 20
--Kanval | 22 | 29
----Omera | 23 | 24
----Dafiyah | 25 | 26
----Daneen | 27 | 28
--Lamya | 30 | 43
----Banujah | 31 | 32
----Buthaynah | 33 | 34
----Vardah | 35 | 36
----Kaneez | 37 | 38
----Parveen | 39 | 40
----Ghunyah | 41 | 42
--Qamar | 44 | 53
----Deeba | 45 | 46
----Pakeezah | 47 | 48
----Rabiya | 49 | 50
----Banafsha | 51 | 52
--Wafiyah | 54 | 61
----Asheeyana | 55 | 56
----Hutun | 57 | 58
----Aakifah | 59 | 60
I have Joe Celko's Trees and Hieracrchies in SQL for Smarties, it has examples of moving nodes around like moving Chaman to the front. I have also found similar examples on the web, but I cannot find any SQL examples that will sort all the siblings.
How might one go about sorting the siblings?
Details on how I created the above data...
I have a test app that will populate the nested set. So I simply created it twice, one time with the names out of order, the second time with the names in order to show the desired results. As far as actually getting this data out of the database, I used this query:
SELECT COUNT(e1.ObjectId) AS LEVEL, e2.name, e2.lft, e2.rgt
FROM EventNode AS e1, EventNode AS e2
WHERE e2.lft BETWEEN e1.lft AND e1.rgt
GROUP BY e2.ObjectId
ORDER BY e2.lft
The reason why ordering is important is because the order is going to be controlled by the end user. They will be able to sort either way and also move individual nodes around so the siblings are displayed in the order they desire. So it is important that the data in the tree be in the correct order.
(P.S. in the real data there is an ObjectID which is a unique identifier, it allows the names to be repeated within the nested set)
Considering your table has the following structure:
I'd first get the parentObjectID of each Node and then re-generate the Nested Set with the needed lft and rgt values. I have also re-ordered alphabetically the children on level 3 and inserted the sample data into EventNode:
Now, querying the initial table returns the following results: