Yii Framework Forum: Xml Sorting Problem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Xml Sorting Problem Rate Topic: -----

#1 User is offline   germanliu 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 26-September 12

Posted 16 October 2012 - 11:00 AM

Hi, I am loading an XML with DOMDocument. The XML has prices which come like: <![CDATA[ €380,000 ]]> for Euros and like: <![CDATA[ $3,000 ]]> for Dollars.

I am inserting the nodeValues into and array to use with CListView widget. The problem is that when I sort by price the sorting is not correct. I believe the problem is that sorting is done on a string basis and not on a numeric basis because it makes $3,000 > $10,000.

Any idea on how to solve this?

Thanks in advance!
German
0

#2 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 16 October 2012 - 12:34 PM

Where and how do you sort? Chances are indeed that those cdata fragments are sorted as strings and hence in lexicographic order.
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#3 User is offline   germanliu 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 26-September 12

Posted 16 October 2012 - 12:41 PM

Hi, thanks for your reply.

I sort with the CListView widget. I use a CArrayDataProvider.

I agree with you, the thing is I can't find a way to transform '€'380,000 into a number. Any ideas?
0

#4 User is offline   germanliu 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 26-September 12

Posted 16 October 2012 - 12:43 PM

Actually I the price in the XML comes like this: & # 8 3 6 4 ; 380,000 (all together) for Euros and & # 3 6 ; 295,000 (all together)for Dollars
0

#5 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 17 October 2012 - 06:27 AM

You would need to find a way to strip the currency entity and the number formatting. Is there any reason why they are already in the source XML?
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#6 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 17 October 2012 - 07:19 AM

I guess you could try something like this, assuming that the currency always uses the same character to represent the decimal point:

// Remove any character that is not numeric or a decimal point
$stripped = preg_replace('/[^0-9.]/', '', $original);


If you want to handle the whole array, $original can be replaced with an array of values and $stripped will be returned as an array.

EDIT:
I suggest you build up an additional column in your array (to hold the numeric values) and use one of the usort functions to perform a custom sort. You may need to cast the values in this column to doubles in order to assure numeric sorting, so don't use the values for anything other than the sort.
0

#7 User is offline   germanliu 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 26-September 12

Posted 17 October 2012 - 08:01 AM

To both of you, thank you very much!!! I was thinking the solution might have been something of this type. It's good to share doubts, hopes it helps someone else!
0

#8 User is offline   Johnny Gan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 30-May 13

Posted 28 October 2013 - 08:45 AM

XML is a linear data structure, so sorting is not easy to do directly. But if you convert it into array, then it's much easier to sort. Of course, you have to convert it back to XML after sorting.

Most people will like to use simpleXML to convert XML to array, and here is some links to convert array into XML:
http://stackoverflow...ay-to-simplexml
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users