Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » How to sort a vector?
How to sort a vector? [message #37842] Fri, 16 November 2012 22:41 Go to next message
crydev is currently offline  crydev
Messages: 151
Registered: October 2012
Location: Netherlands
Experienced Member
I searched all over the forum and documentation and I used Visual Studio intellisense to help me, but I just cannot understand how I should sort a Vector. The Vector in this case is a Vector<MovieInfo>, which is a struct I wrote myself.

I found the different Sort functions but I simply don't understand how they work. I am used to using predicates, delegates and operators using C# to sort arrays and find values in one.

The problem is that I am using a Vector of items with an ArrayCtrl containing those items. I sorted the ArrayCtrl, which makes the indices different from the Vector. To sort my ArrayCtrl I simply used:

ArrayCtrl.Sort();


Which sorted alphabetically. I need to have the same for my Vector.
Can you help me?
Re: How to sort a vector? [message #37844 is a reply to message #37842] Sat, 17 November 2012 00:34 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello crydev

If MovieInfo has operator< it is easy, just call Sort(myvector);

If not, you can do this:

struct MySortWay {
	bool operator () (const MovieInfo& a, MovieInfo& b) const { 
		return a.value < b.value; // Just an example. It dependes on MovieInfo contents
	}
};

Vector <MovieInfo> myvector;

Sort(myvector, MySortWay);


Smile


Best regards
Iñaki
Re: How to sort a vector? [message #37852 is a reply to message #37844] Sun, 18 November 2012 12:27 Go to previous messageGo to next message
crydev is currently offline  crydev
Messages: 151
Registered: October 2012
Location: Netherlands
Experienced Member
koldo wrote on Sat, 17 November 2012 00:34

Hello crydev

If MovieInfo has operator< it is easy, just call Sort(myvector);

If not, you can do this:

struct MySortWay {
	bool operator () (const MovieInfo& a, MovieInfo& b) const { 
		return a.value < b.value; // Just an example. It dependes on MovieInfo contents
	}
};

Vector <MovieInfo> myvector;

Sort(myvector, MySortWay);


Smile


Thanks Koldo, this makes sense to me. I tried it using the operator < which I created inside MovieInfo:

bool operator < (const MovieInfo& pComp) const
{
	return pComp.cFullPath > cFullPath;
};


However, the two Sort methods do not have the same algorithm! I have tried to find out another way of fixing my problem but since I am using callbacks to fill up my ArrayCtrl one item by another from another thread I'm having trouble finding out how I could sort on the fly.

The ArrayCtrl.Sort() and Sort(myVector) both sort alphabetically, but not exactly the same. What should I do to abandon this problem? Is there a way maybe to sort on the fly so I can keep my callback system?
Re: How to sort a vector? [message #37863 is a reply to message #37852] Mon, 19 November 2012 09:02 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Crydev

What is the best way for you, the ArrayCtrl system or the Sort system?


Best regards
Iñaki
Re: How to sort a vector? [message #37874 is a reply to message #37863] Mon, 19 November 2012 14:46 Go to previous messageGo to next message
crydev is currently offline  crydev
Messages: 151
Registered: October 2012
Location: Netherlands
Experienced Member
koldo wrote on Mon, 19 November 2012 09:02

Hello Crydev

What is the best way for you, the ArrayCtrl system or the Sort system?


Hi Koldo,

The Sort(myVector) system is the best way for me. I noticed that this method has a better way of handling differences in string lengths and also in strings that contain numbers.

Crydev
Re: How to sort a vector? [message #37875 is a reply to message #37874] Mon, 19 November 2012 16:09 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Crydev

AFAIK ArrayCtrl Sort() uses Value comparison that in case of Strings uses DefaultLanguageCompare() function in LangInfo.cpp. However String comparison uses String0::Compare() in AString.hpp.

If you prefer the second, I suppose that you have to create a comparison function like this;

int MyStringCompare(const Value& a, const Value& b) {
	return a.ToString() < b.ToString();
}


and refer to it in Sort:

array.Sort(columnToSort, MyStringCompare);


I have not tested it. Does it work?


Best regards
Iñaki
Re: How to sort a vector? [message #37893 is a reply to message #37875] Tue, 20 November 2012 16:02 Go to previous messageGo to next message
crydev is currently offline  crydev
Messages: 151
Registered: October 2012
Location: Netherlands
Experienced Member
koldo wrote on Mon, 19 November 2012 16:09

Hello Crydev

AFAIK ArrayCtrl Sort() uses Value comparison that in case of Strings uses DefaultLanguageCompare() function in LangInfo.cpp. However String comparison uses String0::Compare() in AString.hpp.

If you prefer the second, I suppose that you have to create a comparison function like this;

int MyStringCompare(const Value& a, const Value& b) {
	return a.ToString() < b.ToString();
}


and refer to it in Sort:

array.Sort(columnToSort, MyStringCompare);


I have not tested it. Does it work?


I tested this. It did work by meaning of: it works, but it didn't work by meaning of it did what I was expecting.

You are saying that the sorting of strings in the ArrayCtrl is done by using the default language rules for strings and the sorting of a vector by using the operator you provided is done by using standard english rules. Am I correct? If so, I think I would need the language specific way of sorting for my vector.
Re: How to sort a vector? [message #37894 is a reply to message #37893] Tue, 20 November 2012 16:15 Go to previous message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Crydev

If you want to be sure the best thing is to read the documentation and comparison functions source and, after that, to do some tests.

Both ArrayCtrl and Sort() are so versatile that you can use the algorithm that you prefer in any of them.


Best regards
Iñaki
Previous Topic: Is libXi needed ??
Next Topic: To add serialize/xmlize/jsonize functions to EditField and others
Goto Forum:
  


Current Time: Fri Mar 29 03:22:13 CET 2024

Total time taken to generate the page: 0.01360 seconds