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

null
    • Ninox partner
    • RoSoft_Steven.1
    • 12 days ago
    • Reported - view

    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}]
    
      • gold_cat
      • 12 days ago
      • Reported - view

      Hi, thanks for Steven’s help. I have another question: how do I set the reverse order? I remember that adding a ‘-’ would work, but it gave an error this time.

       

      ....
      data order by -Nb
      
    • John_Halls
    • 12 days ago
    • Reported - view

    Try

    data order by -number(Nb)
    

    Regards John

      • gold_cat
      • 12 days ago
      • Reported - view

       Thanks!

    • Fred
    • 12 days ago
    • Reported - view

    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.

      • gold_cat
      • 12 days ago
      • Reported - view

       Thanks, Fred! I reinforced this knowledge once again.