Another sort and reorder question

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
klownboy
Posts: 4139
Joined: 28 Feb 2012 19:27

Another sort and reorder question

Post by klownboy »

Hi, I have another sort and reorder question. I apologize up front for the lengthy post. After some lost sleep and fried brain cells, I was finally able to come up with a solution, but it's probably not the most elegant or tightest coding...for sure. Maybe you can point me in the right direction for a more direct method. I have a $build_data list built within a script, but for ease of testing I made in a heredoc in the last code block.

Code: Select all

F:\Wallpaper\|307
F:\Wallpaper\Colorado\|308
F:\Wallpaper\Colorado\June\deep\|307
F:\Wallpaper\|306
F:\Wallpaper\|308
F:\Wallpaper\Colorado\June\deep\|308
F:\Wallpaper\Colorado\June\|306
F:\Wallpaper\Colorado\|306
F:\Wallpaper\Colorado\|307
F:\Wallpaper\Colorado\June\|307
F:\Wallpaper\Colorado\June\deep\|306
F:\Wallpaper\Colorado\June\|308
My goal is to have the folders all sorted/matched or grouped together and also have the current view ($view_cid) as the first one in the list for each folder group.
Formatlist will give me the folders nicely sorted.

Code: Select all

	$build_data = formatlist($build_data, "se", "<crlf>");

Code: Select all

F:\Wallpaper\|306
F:\Wallpaper\|307
F:\Wallpaper\|308
F:\Wallpaper\Colorado\|306
F:\Wallpaper\Colorado\|307
F:\Wallpaper\Colorado\|308
F:\Wallpaper\Colorado\June\|306
F:\Wallpaper\Colorado\June\|307
F:\Wallpaper\Colorado\June\|308
F:\Wallpaper\Colorado\June\deep\|306
F:\Wallpaper\Colorado\June\deep\|307
F:\Wallpaper\Colorado\June\deep\|308
This is the desired result below. As in my previous reorder problem here viewtopic.php?f=3&t=18572, the $view_cid can change (instead of 308 it could be 307 etc). I want $view_cid as the lead view in each folder group, but I don't care about the order of the remaining items.

Code: Select all

F:\Wallpaper\|308
F:\Wallpaper\|306
F:\Wallpaper\|307
F:\Wallpaper\Colorado\|308
F:\Wallpaper\Colorado\|306
F:\Wallpaper\Colorado\|307
F:\Wallpaper\Colorado\June\|308
F:\Wallpaper\Colorado\June\|306
F:\Wallpaper\Colorado\June\|307
F:\Wallpaper\Colorado\June\deep\|308
F:\Wallpaper\Colorado\June\deep\|306
F:\Wallpaper\Colorado\June\deep\|307
From there, the method I came up with involves first grouping the same folders together and then rearranging and place the folder with in the specified view first. After this is done for each folder grouping, the groupings are combined. Seems like a bunch of work...it is fast enough though. Within the script I do have another variable of the $paths (in this case it would be 4 paths also <crlf> separated).

Code: Select all

 $build_data=<<<DAT
F:\Wallpaper\|307
F:\Wallpaper\Colorado\|308
F:\Wallpaper\Colorado\June\deep\|307
F:\Wallpaper\|306
F:\Wallpaper\|308
F:\Wallpaper\Colorado\June\deep\|308
F:\Wallpaper\Colorado\June\|306
F:\Wallpaper\Colorado\|306
F:\Wallpaper\Colorado\|307
F:\Wallpaper\Colorado\June\|307
F:\Wallpaper\Colorado\June\deep\|306
F:\Wallpaper\Colorado\June\|308
DAT;
   $view_cid = "308";
   $build_data = formatlist($build_data, "se", "<crlf>");
   $paths = "F:\Wallpaper\<crlf>F:\Wallpaper\Colorado<crlf>F:\Wallpaper\Colorado\June<crlf>F:\Wallpaper\Colorado\June\deep";
   echo $build_data;
   $i = 1;$New_Build_Data = "";
   foreach($path, $paths, "<crlf>","e") {
      if(substr($path, -1) != "\") {$path = $path . "\";}
      $new_build_i = "";$new_pathlist_i = "";
      foreach($data, $build_data,"<crlf>", "e") {
         if($path LikeI gettoken($data, "1", "|")) {
            $new_build_i = $new_build_i . $data . "<crlf>";
         }
         else {continue; }
            $new_viewline_i = formatlist($new_build_i, "nf", "<crlf>", '*|' . $view_cid);
            $new_pathlist_i = $new_viewline_i . "<crlf>" . replace($new_build_i, $new_viewline_i);
//         $new_pathlist_i = Reorder($new_pathlist_i, $new_viewline_i); or use highend's Reorder function

      }
      echo NewPathList_$i<crlf 2>$new_pathlist_i;
      $New_Build_Data = $New_Build_Data . $new_pathlist_i;
      $i++;
   }
	$build_data = formatlist($New_Build_Data, "e", "<crlf>");
	echo $build_data;
Any ideas or thoughts on a possibly more direct method? Thanks.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Another sort and reorder question

Post by highend »

From what I've understood (and I didn't look at your script!) you want this:

Code: Select all

F:\Wallpaper\|308
F:\Wallpaper\|306
F:\Wallpaper\|307
F:\Wallpaper\Colorado\|308
F:\Wallpaper\Colorado\|306
F:\Wallpaper\Colorado\|307
...
From that:

Code: Select all

F:\Wallpaper\|307
F:\Wallpaper\Colorado\|308
F:\Wallpaper\|306
F:\Wallpaper\|308
F:\Wallpaper\Colorado\|306
F:\Wallpaper\Colorado\|307
...
I deleted some non necessary entries here. Where $cid (here it's 308) is the first item in each group of folders
and $cid needs to be dynamic.

Is this correct?

If yes, this should work:

Code: Select all

    $build_data=<<<DAT
F:\Wallpaper\|307
F:\Wallpaper\Colorado\|308
F:\Wallpaper\Colorado\June\deep\|307
F:\Wallpaper\|306
F:\Wallpaper\|308
F:\Wallpaper\Colorado\June\deep\|308
F:\Wallpaper\Colorado\June\|306
F:\Wallpaper\Colorado\|306
F:\Wallpaper\Colorado\|307
F:\Wallpaper\Colorado\June\|307
F:\Wallpaper\Colorado\June\deep\|306
F:\Wallpaper\Colorado\June\|308
DAT;
    $cid = "307";

    $build_data  = formatlist($build_data, "se", "<crlf>");
    $sort_by_cid = formatlist($build_data, "f", <crlf>, "*\|$cid");
    $data_to_add = formatlist($build_data, "f", <crlf>, "!*\|$cid");

    $new_build_data = "";
    foreach($item, $sort_by_cid, <crlf>, "e") {
        $escaped = regexreplace(regexreplace($item, "\|\d+"), "([\\.+(){\[^$])", "\$1");
        $matches = regexmatches($data_to_add, "$escaped\|\d+", <crlf>);
        $new_build_data .= $item . <crlf> . $matches . <crlf>;
    }
    $build_data = formatlist($new_build_data, "e", <crlf>);
    text $build_data;
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4139
Joined: 28 Feb 2012 19:27

Re: Another sort and reorder question

Post by klownboy »

Hi highend, I haven't tested your version yet and I have to run out for a little while, but I wanted to let you know first that the $view_cid variable is obtained in the script using this code line

Code: Select all

	$view_cid =replacelist(get("View"), "0|1|2|3|4|5|6|7|8|9", "302|303|304|305|306|307|308|309|313|314","|"); // get the view and then the corresponding #cid.
So yes the small and large tiles have a cid that is 2 digits after the initial '3' (e.g., 313, 314). That #cid is a too entrenched in the script for me to change at this point.
Edit: I think I may have misinterpreted what you were saying above. In testing when using cid numbers like 313 or 314 it works just fine. Thanks highend. I'll send some beers your way tomorrow. :beer:
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Another sort and reorder question

Post by highend »

In testing when using cid numbers like 313 or 314 it works just fine.
Does that mean that it doesn't work with other cid numbers?
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4139
Joined: 28 Feb 2012 19:27

Re: Another sort and reorder question

Post by klownboy »

highend wrote:Does that mean that it doesn't work with other cid numbers?
No, I've not completed testing, but so far it works fine on all of the cids. I mentioned 313 and 314 only because of my misinterpretation - thinking incorrectly that you were only working with the last digit only.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Another sort and reorder question

Post by highend »

that you were only working with the last digit only
No, it should work with any cid, regardless of it's length (at least if it's > 0) and last number
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4139
Joined: 28 Feb 2012 19:27

Re: Another sort and reorder question

Post by klownboy »

Thanks highend. For the hell of it I did some speed test your version versus my posted version and the difference in execution time was about 5 ms. Not enough to get excited about. :beer: Some beers are on the way. :beer:
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Another sort and reorder question

Post by highend »

Hehe. No, ofc speed difference is neglectable on such a small sample. Even with your nested foreach loops.

I did a little more stress testing. 999 entries in $build_data. Ofc I had to slightly modify your code (333 entries
in $paths). So 333 paths with 3 different cids = 999 entries overall

Mine takes 1032 ms to finish, yours takes 63 seconds to finish. But as we all know, speed isn't always the most
important factor. Readability counts as well.

The most complicated line is

Code: Select all

$escaped = regexreplace(regexreplace($item, "\|\d+"), "([\\.+(){\[^$])", "\$1");
which could be easily splitted into two, to make it easier to follow what I'm doing here

Code: Select all

$without_trailing_stuff = regexreplace($item, "\|\d+"); // Remove trailing | + digits
$escaped = regexreplace($without_trailing_stuff , "([\\.+(){\[^$])", "\$1"); // Properly escape it for the usage of regexmatches()
But we all know I'm lazy so I normally don't comment short code snippets (very well) :mrgreen:
And you are experienced enough to get what I'm doing there without many comments
Some beers are on the way.
Thanks a bunch, ähhh, beer! :beer:
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4139
Joined: 28 Feb 2012 19:27

Re: Another sort and reorder question

Post by klownboy »

Yes, I assumed your version would be quite a bit faster on huge lists. Of course we are talking about the number of thumbnail sizes the user has in the cache database for a particular folder. Most people would only have "1" thumbnail size for a folder and its sub folders. This portion of the script script doesn't get used at all if the user only has 1 thumbnail size for the folder. It's also not used if the current folder view is not presently in one of the thumbnail sizes which exist in the database. I haven't decided yet but, I may end up using my version only because I can more easily fix it if something has to be changed. Yours, I have to ask you since I'm still not a regex person. Not that I expect anything to be wrong at least not with yours. :)

I have other menu items where the $build_data variable actually has three entries: "folder | thumb_cid | 1 or 0" depending on whether the folder has an existing cache in the database or not. It works, but I may be asking a followup question. :wink: Thanks again.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Another sort and reorder question

Post by highend »

Yours, I have to ask you since I'm still not a regex person
Just ask, it's not a big deal
I have other menu items where the $build_data variable actually has three entries: "folder | thumb_cid | 1 or 0" depending on whether the folder has an existing cache in the database or not. It works, but I may be asking a followup question
Sounds like the same logic style could be applied apart from a changed regex syntax
One of my scripts helped you out? Please donate via Paypal

Post Reply