Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Sorting differ from DB sort

    Hi Isomorphic ,

    I have a grid with SQL datasource configured. The fetch is sent with an initial sort field and the query is generated fine and the query result is proper sorted as given below.

    This is the mysql workbench snapshot of query with fieldname ordered in ascending order.
    Click image for larger version

Name:	abc.PNG
Views:	140
Size:	9.8 KB
ID:	265849
    The data in developer console is also correctly sorted. This issue is that the data is not correctly sorted in the grid. It shows data in reverse order.

    Snapshot of listgrid.
    Click image for larger version

Name:	abcd.PNG
Views:	104
Size:	4.0 KB
ID:	265850

    It's simple code as given below.

    Code:
    <DataSource ID="code_table" serverType="sql" tableName="my_table">
      <fields>  
           <field name="code_id" type="sequence" primaryKey="true" hidden="true"/>
           <field name="measurement" type="text" />
      </fields>
    </DataSource>


    Code:
    ListGridField field = new ListGridField("measurement","Measurement");

    Even if we do client side sort only the data is sorted wrong. The sorting done by MySQL and sorting displayed on grid is different. Although it's a plain string field but I still tried setting the sortNormalizer and returning the string from there but same issue. Any suggestions?


    Thanks in advance


    #2
    Hi kamet4u,

    please see this longer discussion of the topic.

    Best regards
    Blama

    Comment


      #3
      Just to make it official - all the information in that other thread applies to this one. 4 is sorting after 4-1/2 because that's how browsers sort text in general (via localCompare()).

      You could add a sortNormalizer to change this behavior, but a better approach for this particular use case is probably just to stop storing a numeric value as a string.

      Comment


        #4
        Thanks alot Blama Isomorphic for guiding in correct direction. I can't change the db field to numeric as it's open text field.
        Referring to the below post I returned a comparator and now I am getting the correct result.
        https://forums.smartclient.com/forum...sortnormalizer
        I tried by overriding the sort nomalizer as given below.

        Code:
        field.setSortNormalizer((record, fieldName) -> {
        Comparator<String> comparator = (o1, o2) -> o1.compareTo(o2);
        return comparator;
        });
        Could you please answer the below:

        If I log anything inside the body of comparator that log is not getting printed.
        Also even if I return any integer from comparator I am getting the correct sorted result. Looks like the comparator body is ignored.
        Last edited by kamet4u; 6 Jul 2021, 02:16.

        Comment

        Working...
        X