Jump to content

Recommended Posts

Good day all,

 

I'm developing a web portal for our finance users to be able to see the Job Queue in Hyperion. This Job Queue window shows what job will run next. Now, we're using text files to determine what jobs do the users want to be executed. These text files are saved in a folder.

 

I created a script which uses OPENDIR() and READDIR() to check for the textfiles and output the list of text files into the Job Queue window. Now, the sorting of these files are in alphabetical order which should not be because the Hyperion Job Queue is based on the rules set up by the Hyperion developers.

 

The current sorting of the list on the Job Queue window is : ActMMvar.txt, ActSMvar.txt, StatMMvar.txt, StatSMvar.txt

The correct sorting should be : ActSMvar.txt, ActMMvar.txt, StatSMvar.txt, StatMMvar.txt

 

I tried using an array and assign keys to each filenames, and then sort them by their keys: 0 = ActSMvar.txt, 1 = ActMMvar.txt, 2 = StatSMvar.txt, 3 = StatMMvar.txt

but it seems that the default sorting of READDIR() overrides the sorting I did in the array.

 

May I know how to fix this?

 

 

Thanks in advance.

use a custom user sort function

<?php
$data = array('ActMMvar.txt', 'ActSMvar.txt', 'StatMMvar.txt', 'StatSMvar.txt');
// custom sort
usort($data, 'mysort');
// show result
echo '<pre>'.print_r($data, 1).'</pre>';

// the custom sort function
function mysort($a,$b)
{
    $x = strcmp(substr($a,0,3), substr($b,0,3));   //ASC sort
    if ($x==0) {
        return strcmp(substr($b,3,2), substr($a,3,2));    // DESC sort
    }
    else return $x;
}

?>

Good day all,

 

The code Barand provided is good. It does what I was expecting it to do. Now I have another issue:

 

In the code below, I have two functions: readDirectory_Triggers(for listing the trigger files), and sortTriggers(for sorting the trigger files based on priority). Now the code below works fine when I only use one function. That is sortTriggers(), but when I call the USORT($fileArray, 'sortTriggers') inside another function just like the code below, the page just shows blank.

 

May I know what's causing this issue?

 

Thank you very much.

 

<?php
FUNCTION readDirectory_Triggers()
{
$dir = "../../../../CorpRpts/Control/";
$fileArray = ARRAY();
$triggerCount = COUNT(GLOB("$dir*var.txt"));
$keyCount = 0;

// Open a known directory, and proceed to read its contents

IF (IS_DIR($dir)) 
{
	IF($triggerCount > 0)
	{
		$fileArray = GLOB("$dir*var.txt");

		USORT($fileArray, 'sortTriggers');

		WHILE($keyCount <= ($triggerCount - 1))
		{
			$dModified = FILEMTIME($fileArray[$keyCount]);
			IF(BASENAME($fileArray[$keyCount]) == "ActSMvar.txt")
			{
				PRINT "<font size=2><b>Actual Single Month : </b>".BASENAME($fileArray[$keyCount])." : ".DATE("M d, Y @ h:ia", $dModified)."</br>";			
			}
			ELSE IF(BASENAME($fileArray[$keyCount]) == "ActMMvar.txt")
			{
				PRINT "<font size=2><b>Actual Multi-Month : </b>".BASENAME($fileArray[$keyCount])." : ".DATE("M d, Y @ h:ia", $dModified)."</br>";			
			}
			ELSE IF(BASENAME($fileArray[$keyCount]) == "StatSMvar.txt")
			{
				PRINT "<font size=2><b>Statistical Single Month : </b>".BASENAME($fileArray[$keyCount])." : ".DATE("M d, Y @ h:ia", $dModified)."</br>";			
			}
			ELSE IF(BASENAME($fileArray[$keyCount]) == "StatMMvar.txt")
			{
				PRINT "<font size=2><b>Statistical Multi-Month : </b>".BASENAME($fileArray[$keyCount])." : ".DATE("M d, Y @ h:ia", $dModified)."</br>";			
			}			
			$keyCount++;
		}
	}
	ELSE
	{
		PRINT "</br><font size=3 color=red><b><center>Job queue is empty.</center></b></font></br>";
	}
}
}

FUNCTION sortTriggers($a,$b)
{
    $ascSort = STRCMP(SUBSTR(BASENAME($a),0,3), SUBSTR(BASENAME($b),0,3));
    IF ($ascSort==0) {
        RETURN STRCMP(SUBSTR(BASENAME($b),3,2), SUBSTR(BASENAME($a),3,2));
    }
   ELSE RETURN $ascSort;
}
?>

Add

else echo BASENAME($fileArray[$keyCount]) . '<br/>';

 

after your "ELSE IF" statements to see what the filenames are.

 

For the sort function to work the array needs to contain the basenames otherwise it sorts on parts of the path.

Thanks Barand.

 

The issue is fixed. I did a little change in the function call in USORT(). Since this code will be added in a class file, I included the class name together with the funtion sortTriggers during the USORT() call.

 

USORT($fileArray, ARRAY(READDIRECTORY,"sortTriggers"));

 

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.