Sorting a vector of strings into numerical order.


 
Thread Tools Search this Thread
Top Forums Programming Sorting a vector of strings into numerical order.
# 1  
Old 01-29-2011
Sorting a vector of strings into numerical order.

I have a vector of strings that contain a list of channels like this:

Code:
101,99,22HD,432,300HD

I have tried using the sort routine like this:

Code:
sort(mychans.begin(),mychans.end());

For some reason my channels are not being sorted at all. I was hoping someone might have some input that might help me come to a resolution to this problem.

Thank you! Smilie

Last edited by radoulov; 01-29-2011 at 06:41 PM.. Reason: Code tags!
# 2  
Old 01-29-2011
What language is this?
# 3  
Old 01-29-2011
Solution.

The language is C++ and the solution is this: I copy pasted directly in to my code and it worked great.

struct myLessThan : public binary_function< string, string, bool > { bool operator()( first_argument_type e1, second_argument_type e2 ) const { // TODO 1: // optionally preprocess e1 and e2 here and strip the "HD" postfix // or consider it for the actual comparison later on // TODO 2: // use an alternative string-to-int conversion istringstream is1(e1), is2(e2); int i1, i2; is1 >> i1; is2 >> i2; return i1 < i2; } }; // use the functor like this: sort(mychans.begin(),mychans.end(),myLessThan());
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting strings in reverse order

Hello, I have a large database of words and would like them sorted in reverse order i.e. from the end up. An example will make this clear: I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem... (5 Replies)
Discussion started by: gimley
5 Replies

2. Shell Programming and Scripting

how to extract data from numbered files using linux in the numerical order-

Hi experts, I have a list of files containing forces as the only number as follows. Force1.txt Force2.txt Force3.txt Force4.txt Force5.txt . . . . . . . . . Force100.txt I want to put all the data(only a number ) in these forces files in the file with the same order like 1,2,3 ..100 .... (2 Replies)
Discussion started by: hamnsan
2 Replies

3. UNIX for Dummies Questions & Answers

How to sort a column based on numerical ascending order if it includes e-10?

I have a column of numbers in the following format: 1.722e-05 2.018e-05 2.548e-05 2.747e-05 7.897e-05 4.016e-05 4.613e-05 4.613e-05 5.151e-05 5.151e-05 5.151e-05 6.1e-05 6.254e-05 7.04e-05 7.12e-05 7.12e-05 (6 Replies)
Discussion started by: evelibertine
6 Replies

4. Programming

Sorting a multidimensional vector by a specific field.

In some cases I would like to sort by index, in some cases by color and in some cases by Callsign. Can this be done? :D vector< vector<string> > table; vector<string> row; row.push_back("1");row.push_back("green");row.push_back("alpha"); table.push_back(row);... (0 Replies)
Discussion started by: sepoto
0 Replies

5. Programming

Multidimensional array of strings with vector.

I've been struggling with this for quite some time. I decided I should get some help with this. Nothing is working. I'm getting a segmentation fault or out of bounds error when I try to load the entries in the for loop.I'm really frustrated. :mad: Compiling isn't the problem. It's crapping out on... (5 Replies)
Discussion started by: sepoto
5 Replies

6. UNIX for Dummies Questions & Answers

moving/copying files in a numerical order

Hi I am newbie to unix scripting, but i have enough knowledge to understand. I have a specific questions like, I use to collect like 3500 files per experiment, each one named like data_001.img.. data_002.img data_003.img .... data_3500.img I would like to move every 12 files in the 3500... (3 Replies)
Discussion started by: wpat
3 Replies

7. Shell Programming and Scripting

COMM and Numerical Sorting

Hi, Hope someone can shed some light on this... I have two lists of numbers I am comparing using COMM. If the first list is empty, and I sort both lists like so: DIFF=`comm -3 <(echo "$EMPTY" | sort -n) <(echo "$NUMBERS" | sort -n)`I get the error: comm: file 2 is not in sorted order But... (2 Replies)
Discussion started by: LostInTheWoods
2 Replies

8. UNIX for Dummies Questions & Answers

Moving files out of multiple directories and renaming them in numerical order

Hi, I have 500 directories each with multiple data files inside them. The names are sort of random. For example, one directory has files named e_1.dat, e_5.dat, e_8.dat, etc. I need to move the files to a single directory and rename them all in numerical order, from 1.dat to 1000(or some... (1 Reply)
Discussion started by: renthead720
1 Replies

9. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies

10. Shell Programming and Scripting

Listing files in numerical order

Hi, I'm trying to write a ksh script to copy a specified number of files from one directory to another. The files are named in the convention <switchname>_log.<num> and the numbers are sequential single digit onwards. I figured I could find some parameter for ls which would list the files in... (3 Replies)
Discussion started by: Steve_H
3 Replies
Login or Register to Ask a Question