sort text file by first number of the line

hi
can someone help me plz

i have a text file like this
0 1 2 C
10 1 2 S
7 1 2 C
11 1 2 S
9 3 43 C
10 3 43 S
1 3 43 C
101 3 43 S


with this code

ifstream in("fout2.txt");
if (in) {
vector<string> lines;
string line;
while (getline(in, line))
lines.push_back(line);
sort(lines.begin(), lines.end(), [](const string& a, const string& b) {
// Use the first word for comparison
return a.substr(0, a.find_first_of(' ')) <
b.substr(0, b.find_first_of(' '));
});
for_each(lines.begin(), lines.end(), [](const string& s) {
std::cout << s << '\n';
});
}

i obtaint this result

0 1 2 C
1 3 43 C
10 1 2 S
10 3 43 S
11 1 2 S
101 3 43 S
7 1 2 C
9 3 43 C

but i want a result like this :

0 1 2 C
1 3 43 C
7 1 2 C
9 3 43 C
10 1 2 S
10 3 43 S
11 1 2 S
101 3 43 S

thanks
Well, I don't know how you managed to get that code to work! But you need do a numerical comparison, at the moment you're doing a string comparison which is why you are getting:

0
1
10
10
11
101
7
9

Try the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

bool isnotdigit(char c)
{
   return !isdigit(c);
}

bool compare(const string s1, const string s2)
{
   auto itr1 = s1.begin(), itr2 = s2.begin();

   if (isdigit(s1[0]) && isdigit(s2[0]))
   {
      int n1, n2;
      stringstream ss(s1);
      ss >> n1;
      ss.clear();
      ss.str(s2);
      ss >> n2;

      if (n1 != n2)
         return n1 < n2;

      itr1 = find_if(s1.begin(), s1.end(), isnotdigit);
      itr2 = find_if(s2.begin(), s2.end(), isnotdigit);
   }

   return lexicographical_compare(itr1, s1.end(), itr2, s2.end());
}

int main()
{
   ifstream in("fout2.txt");

   if (in)
   {
      vector<string> lines;
      string line;
		
      while (getline(in, line))
         lines.push_back(line);

      sort(lines.begin(), lines.end(), compare);

      for (auto itr = lines.begin(); itr != lines.end(); ++itr)
         cout << *itr << endl;
   }

   return 0;
}
Using your code before, you could do it as well simply by modifying your lambda function that you used in std::sort to this:
1
2
3
4
[](const std::string& a, const std::string& b) {
    std::stoi(a.substr(0, a.find_first_of(' '))) <
    std::stoi(b.substr(0, b.find_first_of(' ')));
}


See http://www.cplusplus.com/reference/string/stoi/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <map>
#include <fstream>

int main()
{
    std::ifstream file( "fout2.txt" ) ;
    std::multimap<int,std::string> mmap ;

    int head ;
    std::string tail ;
    while( file >> head && std::getline( file, tail ) )
          mmap.emplace( head, tail ) ;

     for( const auto& p : mmap ) std::cout << p.first << p.second << '\n' ;
}
Topic archived. No new replies allowed.