I wish to have a function return a sorted version of a QList. (The QList is of a custom-type called an "arblip")
I have two ideas how to do this;
a) Either I could copy the whole array, and use qSort, returning the copy.
or
b) Iterate though the items in one list, copying them into a new list.
I'm not sure which is the better approach, and having tried both, I can get neither to work. I suspect its because I'm a neewbie and not understanding how to clone an array, or am getting my pointers mixed up, or both.
My attempt at using qsort was basicaly;
bool compareNames(arblip& s1,arblip& s2)
{
return (temp1<temp2);
}
QList<arblip> arbliparray::getallblips_orderedbytimestamp(){
//create temp list;
QList <arblip> orderedarray;
orderedarray=arblipStore;
qSort(orderedarray.begin(),orderedarray.end(),compareNames);
return orderedarray;
}
bool compareNames(arblip& s1,arblip& s2)
{
QDateTime temp1=s1.getLastUpdateTime();
QDateTime temp2=s2.getLastUpdateTime();
return (temp1<temp2);
}
QList<arblip> arbliparray::getallblips_orderedbytimestamp(){
//create temp list;
QList <arblip> orderedarray;
orderedarray=arblipStore;
qSort(orderedarray.begin(),orderedarray.end(),compareNames);
return orderedarray;
}
To copy to clipboard, switch view to plain text mode
Which compiled, but once that part of the code was triggered, exited with a "1073741819" code.
I suspected its probably that = statement.
I had a similar issue with trying method b, this time crashing when I tried to assign a arblip object from one list to another.
...arblip currentblip = *arblip_iterator;
orderedarray.append(currentblip); ...
...arblip currentblip = *arblip_iterator;
orderedarray.append(currentblip); ...
To copy to clipboard, switch view to plain text mode
I think my problem is not knowing how to make a complete copy of an object, rather then merely creating a new pointer to it?
I want to return a ordered copy of the list, and for the original list to remain private and untouched.
Bookmarks