Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView::sortByColumn(int column, Qt::SortOrder order) Why does this function sort not by numbers but by characters
Forum Updated to NodeBB v4.3 + New Features

QTreeView::sortByColumn(int column, Qt::SortOrder order) Why does this function sort not by numbers but by characters

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 7 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    duncan98
    wrote on last edited by
    #1

    QTreeView::sortByColumn(int column, Qt::SortOrder order)
    Why does this function sort not by numbers but by characters

    eyllanescE 1 Reply Last reply
    0
    • D duncan98

      QTreeView::sortByColumn(int column, Qt::SortOrder order)
      Why does this function sort not by numbers but by characters

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @duncan98 Could you provide a minimal and verifiable example to understand the cause of the "error" and propose a possible solution.

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      D 1 Reply Last reply
      0
      • eyllanescE eyllanesc

        @duncan98 Could you provide a minimal and verifiable example to understand the cause of the "error" and propose a possible solution.

        D Offline
        D Offline
        duncan98
        wrote on last edited by
        #3

        @eyllanesc
        The ascending order of this function is to put 10 before 2, 3, 4, 5, etc., which is obviously sorted by character

        eyllanescE 1 Reply Last reply
        0
        • D duncan98

          @eyllanesc
          The ascending order of this function is to put 10 before 2, 3, 4, 5, etc., which is obviously sorted by character

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by
          #4

          @duncan98 I understand the behavior you describe but the default ordering assuming it is a string, so if you want to change that criterion, you must modify the model, and for that reason I need the example that I asked if you want help.

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          D 1 Reply Last reply
          2
          • eyllanescE eyllanesc

            @duncan98 I understand the behavior you describe but the default ordering assuming it is a string, so if you want to change that criterion, you must modify the model, and for that reason I need the example that I asked if you want help.

            D Offline
            D Offline
            duncan98
            wrote on last edited by
            #5

            @eyllanesc
            I'm going to give up Qt

            JonBJ 1 Reply Last reply
            0
            • D duncan98

              @eyllanesc
              I'm going to give up Qt

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @duncan98 said in QTreeView::sortByColumn(int column, Qt::SortOrder order) Why does this function sort not by numbers but by characters:

              I'm going to give up Qt

              Excellent, far better than providing an example :)

              In any case: it sorts with whatever type is in the column in the model. If that is a number type it will sort numerically, if that is a string type it will sort alphabetically.

              1 Reply Last reply
              3
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Using a custom QSortFilterProxyModel with QCollator using the numeric mode should give you the results you want.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • Seb TurS Offline
                  Seb TurS Offline
                  Seb Tur
                  wrote on last edited by Seb Tur
                  #8

                  I'm also interested in the topic
                  I use QStandardItemModel adding items manually.

                  I want the items to be displayed with coma as decimal separator but then the sorting with QSortFilterProxyModel will consider those cells QStrings.
                  Is that the use case for QCollator @SGaist or is there something easier to have number with local decimals and still be able to sort as numbers?

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • Seb TurS Seb Tur

                    I'm also interested in the topic
                    I use QStandardItemModel adding items manually.

                    I want the items to be displayed with coma as decimal separator but then the sorting with QSortFilterProxyModel will consider those cells QStrings.
                    Is that the use case for QCollator @SGaist or is there something easier to have number with local decimals and still be able to sort as numbers?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Seb-Tur Don't add the values as QString but as numbers. -> QStandardItem::setData(value, Qt::DisplayRole)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    V 1 Reply Last reply
                    3
                    • Christian EhrlicherC Christian Ehrlicher

                      @Seb-Tur Don't add the values as QString but as numbers. -> QStandardItem::setData(value, Qt::DisplayRole)

                      V Offline
                      V Offline
                      Vu Tung Lam
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher said in QTreeView::sortByColumn(int column, Qt::SortOrder order) Why does this function sort not by numbers but by characters:

                      @Seb-Tur Don't add the values as QString but as numbers. -> QStandardItem::setData(value, Qt::DisplayRole)

                      Yes, thank you. This gets the job done. AI and the above people came up with crazy stuff.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved