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 » Community » Newbie corner » Sorting a ValueArray ?
Sorting a ValueArray ? [message #42911] Mon, 14 April 2014 11:49 Go to next message
mingodad is currently offline  mingodad
Messages: 53
Registered: February 2008
Location: Spain
Member
Hello !

How to sort a ValueArray ? Trying to use Sort() gives this error:

static bool CompareGetDistances(ValueMap &a, ValueMap &b) {return a[5] < b[5];}
...
ValueArray &rows = SomeValueArrayWithValueMaps();
...
Sort(rows, CompareGetDistances);
...
-----
/home/mingo/upp/uppsrc/Core/Algo.h:819:31: error: 'class Upp::ValueArray' has no member named 'Begin'
Sort(c.Begin(), c.End(), less);
-----

Should we have the missing Begin() and End() implemented on ValueArray ?

Thanks in advance for any help !

[Updated on: Mon, 14 April 2014 11:54]

Report message to a moderator

Re: Sorting a ValueArray ? [message #42916 is a reply to message #42911] Mon, 14 April 2014 13:46 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mingodad wrote on Mon, 14 April 2014 09:49
Hello !

How to sort a ValueArray ? Trying to use Sort() gives this error:


Right now, you would have to create Vector<Value> from elements, Sort, create new ValueArray.

It is pretty untypical requirement.

Quote:

Should we have the missing Begin() and End() implemented on ValueArray ?


Would not really help as they would have to be const. It is impossible to make non-const references to ValueArray elements available. The reason is that if allowed, it is pretty simple to create referential cycles, resulting in memory leaks.

Think: ValueArray va; va.Add(); va[0] = va;

Mirek
Re: Sorting a ValueArray ? [message #42921 is a reply to message #42916] Mon, 14 April 2014 17:16 Go to previous messageGo to next message
mingodad is currently offline  mingodad
Messages: 53
Registered: February 2008
Location: Spain
Member
After reading your answer and thinking a bit I've got this code to work:
static bool CompareGetDistances(Value &a, Value &b)
{
	return a[5] < b[5];
}
...
    ValueArray &rows = SomeValuaArrayOfValueMap();
    Vector<Value> &vv = const_cast< Vector<Value>& >(rows.Get());
    Sort(vv, CompareGetDistances);
...


Thanks for your help !
Re: Sorting a ValueArray ? [message #42927 is a reply to message #42921] Mon, 14 April 2014 20:21 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mingodad wrote on Mon, 14 April 2014 15:16
After reading your answer and thinking a bit I've got this code to work:
static bool CompareGetDistances(Value &a, Value &b)
{
	return a[5] < b[5];
}
...
    ValueArray &rows = SomeValuaArrayOfValueMap();
    Vector<Value> &vv = const_cast< Vector<Value>& >(rows.Get());
    Sort(vv, CompareGetDistances);
...


Thanks for your help !


That is not quite a good idea. If there exists a copy of ValueArray, it will get sorted too...

To stay on the safe side, I would probably rather used something like

	Vector<Value> v = clone(va.Get());
	Sort(v);
	va = ValueArray(pick(v));


(but thinking about the whole issue, it looks like "Pick" method would not be a bad idea too here...)

Mirek
Previous Topic: Testscatter2 issue
Next Topic: Convert C# code to U++?
Goto Forum:
  


Current Time: Fri Mar 29 01:46:43 CET 2024

Total time taken to generate the page: 0.02544 seconds