Jump to content

number sorting ...


phporcaffeine

Recommended Posts

I have an XML file that has nodes in it like this:

 

<net>-1.0112</net>

<net>1.54</net>

<net>-0.0112</net>

 

... etc

 

Now I am parsing them into a stack (which works fine) .... what I want to do is sort that stack, highest to lowest.  The issue I have is that php doesn't seem to sort the numbers correctly.

 

it sees -1.0112 as a higher number than 1.54 ... how do I do this?

 

Thanks in advance

 

Link to comment
Share on other sites

Okay, I am using the floatval() and putting the result as the key in the stack.  Then I sort the stack with sort() and I get close but no cigar:

 

<net>-0.04</net>

<net>-0.05</net>

<net>-0.05</net>

<net>-0.05</net>

<net>-0.05</net>

<net>-0.06</net>

<net>1.54</net>

<net>-0.05</net>

<net>-0.06</net>

<net>-0.07</net>

<net>-0.07</net>

<net>-0.07</net>

<net>-0.08</net>

<net>-0.0905</net>

<net>-0.09</net>

<net>-0.09</net>

<net>-0.09</net>

<net>-0.11</net>

<net>-0.11</net>

<net>1.02</net>

<net>-0.15</net>

<net>-0.17</net>

<net>-0.17</net>

<net>-0.21</net>

 

The goal is to sort the stack highest to lowest using the key to sort with.

Link to comment
Share on other sites

Here is the code:

 

<?php

class xmlreaders {

var $_data;

var $_white;

var $_xml_url;

function xmlreaders ($xml_url = "") {

$this->_white = 1;

if (trim($xml_url) != "") $this->set_xml_url ($xml_url);

}
function set_xml_url ($url) {

$this->_xml_url = $url;

}

function read () {

if (!$this->_xml_url) $this->error ("XML File is not assigned.");

$fp = fopen ($this->_xml_url, "r");

while (!feof ($fp)) $this->_data .= fgets($fp, 4096);

fclose ($fp);

$this->_data = trim ($this->_data);

}

function parse () {

$this->read();

if (trim ($this->_data) == "") $this->error ("Data not ready.");

$vals = $index = $array = array();

$parser = xml_parser_create();

xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $this->_white);

xml_parse_into_struct($parser, $this->_data, $vals, $index);

xml_parser_free($parser);

$i = 0;

$tagname = $vals[$i]['tag'];

if ( isset ($vals[$i]['attributes'])) {

	$array[$tagname]['@'] = $vals[$i]['attributes'];

}

else {

	$array[$tagname]['@'] = array();

}

$array[$tagname]["#"] = $this->xml_depth($vals, $i);

return $array;

}

function xml_depth($vals, &$i) {

$children = array();

if ( isset($vals[$i]['value'])) {

	array_push($children, $vals[$i]['value']);

}

while (++$i < count($vals)) {

	switch ($vals[$i]['type']) {

		case 'open':

			if ( isset ( $vals[$i]['tag'])) {

				$tagname = $vals[$i]['tag'];

			}

			else {

				$tagname = '';

			}

			if ( isset ( $children[$tagname])) {

				$size = sizeof($children[$tagname]); 

			}

			else {

				$size = 0;

			}

			if ( isset ( $vals[$i]['attributes'])) {

				$children[$tagname][$size]['@'] = $vals[$i]["attributes"]; 

			}

			$children[$tagname][$size]['#'] = $this->xml_depth($vals, $i);

			break;

			case 'cdata':

				array_push($children, $vals[$i]['value']);

				break;

			case 'complete':

				$tagname = $vals[$i]['tag'];

				if( isset ($children[$tagname])) {

					$size = sizeof($children[$tagname]);

				}

				else {

					$size = 0;

				} 

				if( isset ( $vals[$i]['value'])) {

					$children[$tagname][$size]["#"] = $vals[$i]['value'];

				}

				else {

					$children[$tagname][$size]["#"] = '';

				}

				if ( isset ($vals[$i]['attributes'])) {

					$children[$tagname][$size]['@'] = $vals[$i]['attributes'];

				}

				break;

				case 'close':

					return $children;

					break;

	}

}

return $children;

}

function traverse_xmlize($array, $arrName = "array", $level = 0) {

foreach($array as $key=>$val) {

	if ( is_array($val)) {

		traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);

	}

	else {

		$GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";

	}

}

return 1;

}

function error ($str) {

print get_class ($this)." ".$this->version()." => $str";

exit();

}

function version () {

return "1.0";

}

}

$xml = new XMLReader();

$xmlurl = "stocksvol.xml";

$xmlreaders = new xmlreaders ($xmlurl);

$xml = $xmlreaders->parse();

$new = array();

foreach ($xml['TTI_transmission']['#']['q'] as $value) {

//FORMULA = $value['#']['s'][0]['#']  (WHERE 'S' IS THE NODE THAT YOU ARE TRYING TO GET THE VALUE OF

$net = $value['#']['l'][0]['#'] - $value['#']['o'][0]['#'];

echo $value['#']['l'][0]['#'] . "-" . $value['#']['o'][0]['#'] . "=" . $net . "<br />";

$num = floatval($net);

$new[] = array($num => "<net>$net</net><q><s>" . $value['#']['s'][0]['#'] . "</s><l>" . $value['#']['l'][0]['#'] . "</l><c>" . $value['#']['c'][0]['#'] . "</c><p>" . $value['#']['p'][0]['#'] . "</p><h>" . $value['#']['h'][0]['#'] . "</h><o>" . $value['#']['o'][0]['#'] . "</o><h>" . $value['#']['h'][0]['#'] . "</h><t>" . $value['#']['t'][0]['#'] . "</t><d>" . $value['#']['d'][0]['#'] . "</d><t>" . $value['#']['t'][0]['#'] . "</t></q>");

}

$date =$xml['TTI_transmission']['#']['datestamp'][0]['#'];

unset($xml);

unset($xmlreaders);

sort($new);

$cnt =0;

$export = "
<?xml version=\"1.0\" ?>
<!DOCTYPE TTI_transmission [
        <!ELEMENT TTI_transmission      (q*,datestamp)>
        <!ELEMENT datestamp     (#PCDATA)>
        <!ELEMENT q   (s?,l?,c?,p?,v?,o?,h?,w?,d?,t?)>
        <!ELEMENT s    (#PCDATA)>
        <!ELEMENT l    (#PCDATA)>
        <!ELEMENT c    (#PCDATA)>
        <!ELEMENT p    (#PCDATA)>
        <!ELEMENT v    (#PCDATA)>
        <!ELEMENT o    (#PCDATA)>
        <!ELEMENT h    (#PCDATA)>
        <!ELEMENT w    (#PCDATA)>
        <!ELEMENT d    (#PCDATA)>
        <!ELEMENT t    (#PCDATA)>
]>
<TTI_transmission>
";

foreach ($new as $key => $value) {

foreach ($value as $val) {

	$cnt++;

	if ($cnt >= 100) {
		break;

	}

	$export .= $val . "\n";

}

}

$export .= "<datestamp>" . $date . "</datestamp>
</TTI_transmission>";

fwrite(fopen('export.xml', 'w+'), $export);

?>

Link to comment
Share on other sites

I'll assume you are referring to these lines of code -

 

   $num = floatval($net);
   
   $new[] = array($num => "<net>$net</net><q><s>" . $value['#']['s'][0]['#'] . "</s><l>" . $value['#']['l'][0]['#'] . "</l><c>" . $value['#']['c'][0]['#'] . "</c><p>" . $value['#']['p'][0]['#'] . "</p><h>" . $value['#']['h'][0]['#'] . "</h><o>" . $value['#']['o'][0]['#'] . "</o><h>" . $value['#']['h'][0]['#'] . "</h><t>" . $value['#']['t'][0]['#'] . "</t><d>" . $value['#']['d'][0]['#'] . "</d><t>" . $value['#']['t'][0]['#'] . "</t></q>");

...

sort($new);

 

You are setting the array index to $num. sort() is sorting the values (not the index) and it in fact replaces the indexes -

Note: This function assigns new keys to the elements in array . It will remove any existing keys that may have been assigned, rather than just reordering the keys.

 

You should probably look at krsort() - krsort

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.