My problem is that i've got an input file which contains information about universities, contains string, integer, floating points types also... Here is the first three lines of the file, how could i for example sort it by institution name?? Each column is separated by the tab character... Please help me...
World Rank Institution Region Regional Rank Country National Rank Score on Alumni Score on Award Score on HiCi Score on N&S Score on SCI Score on Size Total Score
1 Harvard Univ Americas 1 USA 1 100 100 100 100 100 72.4 100
2 Univ Cambridge Europe 1 UK 1 99.8 93.4 53.3 56.6 70.9 66.9 73.6
3 Stanford Univ Americas 2 USA 2 41.1 72.2 88.5 70.9 72.3 65 73.4

sort the lines just like you would sort any other set of strings, but instead of comparing the strings first find the first tab in the line and compare the strings up to that position

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

struct SAscendingDateSort
{
     bool operator()(string& s1, string& s2)
     {
         size_t pos = s1.find_first_of('\t');
         string ss1 = s1.substr(0,pos);
         pos = s2.find_first_of('\t');
         string ss2 = s2.substr(0,pos);
          return ss1 < ss2;
     }
};

int main()
{
    vector<string> lines;
    
    lines.push_back("Harvard Univ Americas \t 1 USA 1 100 100 100 100 100 72.4 100");
    lines.push_back("Univ Cambridge Europe \t 1 UK 1 99.8 93.4 53.3 56.6 70.9 66.9 73.6");
    lines.push_back("Stanford Univ Americas \t 2 USA 2 41.1 72.2 88.5 70.9 72.3 65 73.4");
    std::sort(lines.begin(),lines.end(),SAscendingDateSort()); 

    for(int i = 0; i < lines.size(); i++)
        cout << lines[i] << "\n";
}

Most likely the above will be a little more then what you really need. Just compare the entire string will sort them correctly too because no two lines will have the same university name. So this would probably be sufficient.

struct SAscendingDateSort
{
     bool operator()(string& s1, string& s2)
     {
          return s1 < s2;
     }
};
This article has been dead for over six months. Start a new discussion instead.