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. Problem in Sorting structure

Problem in Sorting structure

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.7k 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.
  • S Offline
    S Offline
    Swinetha
    wrote on last edited by
    #1

    hai I am implemented one logic to sort array of structure

    Is shown below

    @struct library
    {
    char Nuclide_name[20];
    float Nuclide_energy;
    };@

    @
    bool caseInsensitiveLessThan(const library &n1, const library &n2)
    {
    return n1.Nuclide_energy<n2.Nuclide_energy
    }
    @
    @for(int i=0;i<4;i++)
    qDebug()<< QString::number(Nuclide[i].Nuclide_energy)<<Nuclide[i].Nuclide_name;
    QList<library> list;
    for(int i=0;i<4;i++)
    {
    list.append(Nuclide[i]);
    qDebug()<< QString::number(Nuclide[i].Nuclide_energy)<<Nuclide[i].Nuclide_name;
    }

    qSort(list.begin(),list.end(),caseInsensitiveLessThan);
    qDebug()<<"sorting completed"<<QString::number(list.count());
    for(int i=0;i<4;i++)
    qDebug()<< QString::number(Nuclide[i].Nuclide_energy)<<Nuclide[i].Nuclide_name;@
    

    is there any mistake in my code?
    I wrote this code with refence of this"qsort":http://qt-project.org/doc/qt-4.8/qtalgorithms.html#qSort-2

    please suggest me

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      you're printing the wrong contents:

      the last for loop shall be substituted with

      @
      for(int i=0;i<4;i++)
      qDebug()<< list[i].Nuclide_energy<<list[i].Nuclide_name;
      @

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Swinetha
        wrote on last edited by
        #3

        In list the data is appended but it is not sorted after qSort

        Is qSort() is work for structers or not

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          this code works well for me

          @
          void Widget::on_pushButton_clicked()
          {
          library Nuclide[4];
          for (int i = 0; i < 4; ++i) {
          Nuclide[i].Nuclide_energy = 4 - i;
          sprintf(Nuclide[i].Nuclide_name, "Nuclide_%d", i);
          }

          for(int i=0;i<4;i++)
              qDebug()<< QString::number(Nuclide[i].Nuclide_energy)<<Nuclide[i].Nuclide_name;
          
          QList<library> list;
          for(int i=0;i<4;i++)
          {
              list.append(Nuclide[i]);
              qDebug()<< QString::number(Nuclide[i].Nuclide_energy)<<Nuclide[i].Nuclide_name;
          }
          
          qSort(list.begin(),list.end(),caseInsensitiveLessThan);
          qDebug()<<"sorting completed"<<QString::number(list.count());
          
          for(int i=0;i<4;i++)
              qDebug()<< list[i].Nuclide_energy<<list[i].Nuclide_name;
          

          }
          @

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          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