Click to See Complete Forum and Search --> : Comparing Strings
LaxRoth
February 11th, 2005, 06:21 AM
C;C++
Hi everybody :D
who knows a good solution for sorting strings excel-like?
Must be very performant.
Like to compare strings with number in or whole "numbers".
Example:
a
10
20
110
30
shall be sorted to:
10
20
110
30
a
that means: characters before numbers, numbers by size...
using qsort algorithm, comparing manually.
how to compare 110 with 10? strcmp says 10 is bigger than 110. can explain that.
anyone an idea? is there a base function in string.h or so?
:wave:
NoHero
February 11th, 2005, 06:24 AM
You should make two arrays: One that contains the strings which are numbers (like "10" "110" etc.) in integers and the other one's in strings. You can use the atoi() function to convert a string (which contain a legal integer) into an integer. Sort both arrays and combine them. We call that MergeSort.
LaxRoth
February 11th, 2005, 06:32 AM
Thx for your fast answer. building arrays is not possible in our qsort, we use it also for other things sorting. think i will use sscanf.
Andreas Masur
February 11th, 2005, 06:57 AM
Well....since you said C/C++....use a vector of string and the sort algorithm...
LaxRoth
February 11th, 2005, 06:59 AM
??? don't understand. can you give an example?
Andreas Masur
February 11th, 2005, 07:18 AM
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>
int main()
{
std::vector<std::string> vec;
vec.push_back("10");
vec.push_back("Hallo");
vec.push_back("110");
vec.push_back("Welt");
vec.push_back("104");
std::sort(vec.begin(), vec.end());
for(std::vector<std::string>::iterator iter = vec.begin(); iter != vec.end(); ++iter)
std::cout << *iter << std::endl;
_getch();
}
LaxRoth
February 11th, 2005, 07:36 AM
First review look fine. will see how it works with mass data. thx.
:cool:
LaxRoth
February 11th, 2005, 08:11 AM
ok...doesn't work. look:
007
10
10.1
10.1.1
104
110
110
2
20
3
7
Hallo
Welt
a0
aa
aaaba
aba
2 comes after 10. bad.
:mad:
NoHero
February 11th, 2005, 09:51 AM
Dito. You will need two arrays: One for the real strings and one for the strings which contain integers. Of course you need this array to be an array of integers. Otherwise comparison would not work at all. Another solution: You write your own sorting algorithm which can handle both cases.
If you need a clue which comparison sorts are available, and which should be prefered in which case. (http://www.codeguru.com/forum/showthread.php?t=326194)
/ Please, I don't to remember how these algorithms worked ... :sick:
LaxRoth
February 11th, 2005, 09:59 AM
we implemented the qsort algorithm.
we talk about the compare function here. :thumb:
i get to strings i have to compare, don't know whether they are numbers or not. i have to return -1 if element 1 is smaller etc. (like strcmp)
:sick:
NoHero
February 11th, 2005, 10:01 AM
Take a look at this FAQ (http://www.codeguru.com/forum/showthread.php?s=&threadid=231054). It will help you to differ between "0" and non-digit strings.
I am not that specialist but I do think that it is possible to sort with such criteria with one qsort() call. Because qsort() expects the data to be one format, but the data you have contains two formats.
Andreas Masur
February 11th, 2005, 10:27 AM
ok...doesn't work. look:
Well....thie example I provided simply uses the default sortation criteria...you are free to provide your own sorting criteria which will sort these strings based on your criteria...
LaxRoth
February 12th, 2005, 06:02 AM
Good Morning.
My first post was to find a solution for sorting strings excel-like-
i dont search for a sorting algorithm, i will have to compare the elements on my own.
:wave:
NoHero
February 12th, 2005, 06:26 AM
Good Morning.
My first post was to find a solution for sorting strings excel-like-
i dont search for a sorting algorithm, i will have to compare the elements on my own.
:wave:
And we have provided you all information on how to compare/convert strings to integers. What do you need more?
LaxRoth
February 12th, 2005, 01:21 PM
a function that does compare my strings excel-like...
;)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.