2uzhan.com
Advertisement
Now Place:2uzhan.com » how to sort file by the first number of every line

how to sort file by the first number of every line

Software Development @ December 23, 2013   Views:0

Hi what is the solution ?

i have a 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 :

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 an output 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

--------------Solutions-------------

You could try an algorithm as follows:

Create a new list that has your sorted list.
input_list -> initially has unsorted lines.

while ( !input_list.empty() ) {
  search smallest element, add this to a new list. // this is a O(n) operation.
  delete this entry from input_list.
}

HTH.

The simplest solution is to convert the first token (that is, the first contiguous series of characters) of each line to an integer, and sort by the integers. Given this approach, it would then make more sense to use an STL map(), which automatically sorts itself according to a predicate passed as an optional template parameter, rather than sorting the list after the fact. Since the default sorting uses the less comparison, you may not even need the predicate.

std::map<int, std::string> sorted_strings = std::map<int, std::string>(); 

while (getline(in, line))
{
    std::stringstream ss;
    ss << in;
    int key;
    ss >> key;
    sorted_strings[key] = in;
}
Window x:Class="HexagonWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HexagonWindow"
        mc:Ignorable="d"
        Title="Hexa" Height="300" Width="300" WindowStyle="None"
        AllowsTransparency="True" Background="Transparent" MouseLeftButtonDown="Window_MouseLeftButtonDown">
    <Grid>
        <!--Points="100,0 200,0 300,100 300,200 200,300 100,300 0,200 0,100" use these points for octagon window--> ...

Before I get into the specific issues, I want to provide some other details:

  • Normally, I work on a live, paid server (even in dev); I realize that is ...

Hey there,

I am trying to create a register code. So basicly im making a quiz and every time a user starts up my program he/she needs to register (making ...

Tags:file, sort
© 2018 2uzhan.com Contact