List of JavaScript Objects and Functions



  • Hi everyone,
    I need to sort a JS array of objects like this { productId : "", product : "", price : 0, rebate : "", description: "" }
    This is my code :
    source.sort(function(a,b){
    return a.product.toLocaleLowerCase().localCompare(b.product.toLocaleLowerCase())
    });
    When running, I got this error :
    Starting /home/diarby/studio/qt/projects/build-test-Desktop_Qt_5_10_0_GCC_64bit-Debug/qtc_Desktop_Qt_5_10_0_GCC_64bit_Debug/install-root/test...
    qrc:/datasource.js:1277: TypeError: Property 'localCompare' of object orange is not a function

    Here, orange is the value of product property. and source is the array.
    In the manual, it is said that ECMA-262 is supported and localCompare() is really a function.

    How can I do array sorting on basis of string objects ?


  • Moderators

    @Diarby said in List of JavaScript Objects and Functions:

    qrc:/datasource.js:1277: TypeError: Property 'localCompare' of object orange is not a function

    Here, orange is the value of product property. and source is the array.
    In the manual, it is said that ECMA-262 is supported and localCompare() is really a function.

    Do you mean localeCompare()?



  • Hello, yes I do. Please see my code on top.
    I just copied and pasted "as is" the error message received from the application.


  • Moderators

    @Diarby

    1. localeCompare()
    2. localCompare()

    You need #1 but you wrote #2



  • Oh my God ! Thanks for your help. I was really bad there.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.