Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Sorting Alphanumeric value list

Sorting Alphanumeric value list

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.1k Views
  • 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.
  • M Offline
    M Offline
    manny_lp
    wrote on 29 May 2020, 04:19 last edited by
    #1

    Hi all,
    I have a list in which the value can be alphanumeric, only numeric or only alphabets.
    Which function is suitable to handle all these cases? I am using QSortFilterProxyModel.

    I tried using the following in lessThan() function.

    1. "leftData->primaryIdNo().compare(rightData->primaryIdNo(), sortCaseSensitivity()) < 0 ;"
      Works fine for Strings not containing any digits. Numeric values are not sorted.
    2. "leftData->primaryIdNo() < rightData->primaryIdNo();
      Works for numeric values, but sorting of strings fails due to case sensitivity. For example "aaa" appears After "ZZZ".

    How can I handle all these combinations?

    J 1 Reply Last reply 29 May 2020, 07:13
    0
    • M manny_lp
      29 May 2020, 04:19

      Hi all,
      I have a list in which the value can be alphanumeric, only numeric or only alphabets.
      Which function is suitable to handle all these cases? I am using QSortFilterProxyModel.

      I tried using the following in lessThan() function.

      1. "leftData->primaryIdNo().compare(rightData->primaryIdNo(), sortCaseSensitivity()) < 0 ;"
        Works fine for Strings not containing any digits. Numeric values are not sorted.
      2. "leftData->primaryIdNo() < rightData->primaryIdNo();
        Works for numeric values, but sorting of strings fails due to case sensitivity. For example "aaa" appears After "ZZZ".

      How can I handle all these combinations?

      J Offline
      J Offline
      JonB
      wrote on 29 May 2020, 07:13 last edited by JonB
      #2

      @manny_lp
      This question seems to just be about QSortFilterProxyModel, nothing special about QML.

      As you say, you have two different ways of doing the comparison depending on whether the data is string or number. So you have to execute the correct one depending on whether the data you are looking at is alphabetic or numeric. The Qt model level does not know whether your data is to be treated as letters or numbers, only you do. So, depending, you might have something like:

      bool MySortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
      {
          QVariant &leftData( ... source_left ...);
          QVariant &rightData( ... source_right ...);
          if (isNumericColumn(source_left.column())
              return leftData.toInt() < rightData.toInt();
          else
              return (leftData.toString().compare(rightData.toString(), sortCaseSensitivity()) < 0);
      }
      
      1 Reply Last reply
      1
      • M Offline
        M Offline
        manny_lp
        wrote on 11 Jun 2020, 06:49 last edited by
        #3

        Solution is to use QCollator.
        It goes something like this:
        QCollator collator;
        collator.setCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
        collator.setNumericMode(true);
        return collator.compare(leftData->primaryIdNo(),rightData->primaryIdNo()) < 0;

        J 1 Reply Last reply 16 Jun 2020, 07:43
        1
        • M manny_lp
          11 Jun 2020, 06:49

          Solution is to use QCollator.
          It goes something like this:
          QCollator collator;
          collator.setCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
          collator.setNumericMode(true);
          return collator.compare(leftData->primaryIdNo(),rightData->primaryIdNo()) < 0;

          J Offline
          J Offline
          JonB
          wrote on 16 Jun 2020, 07:43 last edited by JonB
          #4

          @manny_lp said in Sorting Alphanumeric value list:

          collator.setNumericMode(true);

          How would you know whether to set this true or false? Seems to me to beg the question. Hence the need to know whether a give column is to be treated as numeric or alphabetic, as per my algorithm. QCollator deals only in strings, won't work for e.g. dates or (I think) floating point numbers as a generic solution.

          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