473,379 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Sorting of vector according to a string which contains date in a particular format

7
hi all ,

i want to sort the vector according to a string and the string according to which i want to sort the data is date ( in a pre defined format) . Now whats the best way to do it...


i can think of using qsort.. is there any better way to do sorting..

thanks in advance guys..
May 2 '07 #1
9 2201
Ganon11
3,652 Expert 2GB
qsort is probably the best way to perform this sort. Not only is the quicksort a relatively fast sorting algorithm, but qsort has already been written for you, so you don't need to test if it works on your own.
May 2 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
If you have a vector, then use the sort algorithm.

You pass in iterators to the begion and end of the sort range and a binary predicate. A binary predicate here would be a function that that takes two vector elements as arguments and returns true if first arg < last arg. The function determines what < means. Here's where you can use your string.

You will have to #include <algorithm>

Avoid qsort. According top Scott Meyers sorting a vector of a million doubles, using sort with an inline comparison function is 670% faster than qsort since qsort must actually call the comparison function each time.
May 2 '07 #3
kunalg
7
hi ,

thanks for the quick reply

but i am getting following error while using the sort function. Do anyone have any idea how to resolve this error?

Could not find a match for std::sort<std::RandomAccessIterator, std::Compare>(ShiftData*, ShiftData*, bool(ShiftData&,ShiftData&)) needed in CapExtrc::getAdjustTechShift(std::vector<ShiftData >).

i have passed the following argument to the function

sort(techShiftPriority0.begin(),techShiftPriority0 .end(),compareTechShift);

compareTechShift is declared the follwing way:


bool CapExtrc::compareTechShift(techShiftData &a , techShiftData &b);

and vector is declared as follows:

vector<techShiftData> techShiftArray;
May 3 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. sort(techShiftPriority0.begin(),techShiftPriority0 .end(),compareTechShift);
  2.  
I think must need to be:
Expand|Select|Wrap|Line Numbers
  1. sort(techShiftArray.begin(),techShiftArray.end(),compareTechShift);
  2.  
Try that.
May 3 '07 #5
kunalg
7
This code:
Expand|Select|Wrap|Line Numbers
  1. sort(techShiftPriority0.begin(),techShiftPriority0 .end(),compareTechShift);
  2.  
I think must need to be:
Expand|Select|Wrap|Line Numbers
  1. sort(techShiftArray.begin(),techShiftArray.end(),compareTechShift);
  2.  
Try that.
No you get it wrong or actually i posted it wrongly. techShiftArray is just a variable name for the vector and variable name is

vector<techShiftData> techShiftPriority0,techShiftPriority1,techShiftPri ority2;

where i have to sort these three vector.

i am not able to get from the message that why it is saying that it is not able to find and showing only one randomIterator, although i am passing two randomIterator.
May 3 '07 #6
kunalg
7
i am also getting the same error while sorting the

bool compareInt( int a , int b)
{
return (a> b)
}


vector< int > vint;

vint.push_back(2);
vint.push_back(6);
vint.push_back(4);


sort(vint.begin(),vint.end(),compareInt);

any idea guys i am getting this error:


nclude -I/usr1/arbor//oam_src/include -c CapExtrc.cpp -o CapExtrc.o
"CapExtrc.cpp", line 1214: Error: Could not find a match for std::sort<std::RandomAccessIterator, std::Compare>(int*, int*, bool(int,int)) needed in CapExtrc::getAdjustTechShift(std::vector<shiftData >).
May 3 '07 #7
kunalg
7
btw i have solved the problem... the problem is that i am defining comparison inside my class and when i try to pass that function to the sort fuction, then sort function is not able to find that function because the function is defined in the scope of the class.

if anyone needs more explantion please leave a message for me..
May 3 '07 #8
Alm
2
btw i have solved the problem... the problem is that i am defining comparison inside my class and when i try to pass that function to the sort fuction, then sort function is not able to find that function because the function is defined in the scope of the class.

if anyone needs more explantion please leave a message for me..
Hi kunalg,

I am facing a similar problem.

I too made the comparison function out of the class scope after reading your post.

However I get an error that says implicit declaration of function int sort()

Did you face anything like that?

-Alm
May 11 '07 #9
Alm
2
Hi kunalg,

I am facing a similar problem.

I too made the comparison function out of the class scope after reading your post.

However I get an error that says implicit declaration of function int sort()

Did you face anything like that?

-Alm

SORRY MY BAD !! I had not included <algorithm>
May 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John Taylor | last post by:
I have a ListCtrl with 5 columns. The first 4 columns are either strings or integers, but the last column is a string in the format of MM-DD-YYYY. I searched google and also read over the...
9
by: p0wer | last post by:
Let's suppose I have this sample document: <root> <entry id="1" <date>2003-08-03</date> <param_1>5</param_1> <param_2>10</param_2> </entry> <entry id="2"> ...
3
by: Raghuram Banda | last post by:
Hi, I was strucked in a problem, basically I'm working on Sortable Table and the table is created using HTML Tags and one of the fields in the table contain Date in unix format (something like...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.