0
Sort by Nb Numerical Size
let data := [ "{'food': 'bread', 'Nb': 3} {'food': 'apple', 'Nb': 7} {'food': 'rice', 'Nb': 1} {'food': 'banana', 'Nb': 5} {'food': 'milk', 'Nb': 9} " ]; let sortedData := (data) order by Nb; sortedData
Hello everyone, I want to sort the data values according to the size of the Nb values. How should I write the code correctly? Thanks for the reply.
6 replies
-
If you use the correct JSON format this works:
let data := [{ food: "bread", Nb: 3 }, { food: "apple", Nb: 7 }, { food: "rice", Nb: 1 }, { food: "banana", Nb: 5 }, { food: "milk", Nb: 9 }]; data order by Nb
result:
[{"food":"rice","Nb":1},{"food":"bread","Nb":3},{"food":"banana","Nb":5},{"food":"apple","Nb":7},{"food":"milk","Nb":9}]
-
Try
data order by -number(Nb)
Regards John
-
Just remember that JSON data is always treated like text, or technically any. So you have to explicitly convert numbers before using them.
In your first example if you don’t use number(Nb), then it would have sorted 10 right after 1 since it would be treated like text.
Content aside
- Status Answered
- 12 days agoLast active
- 6Replies
- 33Views
-
4
Following