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. Natural sort using QCollator
Forum Updated to NodeBB v4.3 + New Features

Natural sort using QCollator

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.4k Views 2 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.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    I have been looking for any basic example using QCollator to sort naturally a QStringList variable full of filenames like this:

    image1.jpg
    image10.jpg
    image11.jpg
    image2.jpg
    image3.jpg
    

    The result I am expecting is this:

    image1.jpg
    image2.jpg
    image3.jpg
    image10.jpg
    image11.jpg
    

    I appreciate any suggestion. Thanks.


    Qt Developer

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2
      QStringList stuff;
      stuff << "image1.jpg"
            << "image10.jpg"
            << "image11.jpg"
            << "image2.jpg"
            << "image3.jpg";
      
      QCollator coll;
      coll.setNumericMode(true);
      
      std::sort(stuff.begin(), stuff.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
      
      1 Reply Last reply
      3

      • Login

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