0

I have an array of values ($arrayToSort) that is as follows

0f03895b-3db8-4bf5-a1b1-62b47f5dab5c: 500
1af8cd11-e2ae-4bf9-8024-f0d4874b2d74: 1030
1d815a24-06de-4794-a3f0-7d38857fee4b: 2033
3c0454fc-e1e5-43b7-ac03-b746a36d06a1: 1034
3d78d082-c6b7-462e-8f7f-38acdd3de761: 2029
8cea7e76-9873-4a98-903f-2b4af3986796: 2250
9bbf7197-f167-4691-8194-2e8868070bb4: 500
9f1dcc1a-6561-411c-b1a7-6af19efe77f3: 2052
28fb81b0-7162-40fc-bb5b-c266f43f3593: 2214
35eed79a-f879-43fa-952b-806f047d66a8: 3038
41ed4161-077a-4370-a1fb-47ddd112b5df: 6076
50cb8711-0ffb-4da2-97d6-033a948ea7c5: 1030
80b0e919-054f-4d4b-b0b6-3a8e14647879: 1035
88c17b89-5b82-4348-ab03-47d4794412fd: 428
275fd6b6-b880-4e25-bd7d-cd609685e922: 1031
630b8187-edfb-44de-ae23-e81b670c9706: 29
700c8740-9cae-444b-b69c-ad4361bfaf81: 230
5414bfe7-4d9b-41e0-bf56-d6da7534db8c: 29
7262ccf0-09a3-450b-a258-6aa1c1072fb8: 4037
7486a107-390a-43a3-8cc5-b6de094d50cb: 600
b1d3a6a6-b107-4665-ad86-4bd827b8a76e: 29
b6487821-c823-4262-aaaf-e758d38e2826: 214
bfa88b65-3724-47d1-a3c6-055abd568d27: 2333
f47e1e05-da9a-40bf-9b0d-b8f86cbcd522: 2032
fd1bbd8d-21e9-44c3-8916-a54ee3a554cc: 2000

As well as my self defined sorting function

private function arraySort($a, $b) {
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }

When I call my sort function with the above dataset and function using uasort($arrayToSort,array('self','arraySort)); I get back my data in the exact same order. I have tried using my comparison functon with usort, and I get back the values sorted (albeit without the keys of course). For the life of me I cannot see why uasort is failing to sort these values when usort is capable of doing so. Am I calling the function incorrectly?

0

1 Answer 1

2

Your call to uasort seems wrong compared to the code you provided.

If you want to call uasort using a standalone callback, you should use uasort($array, 'arraySort');

If you want to call uasort from within an object instance, while the callback is an instance method, you should use uasort($array, array($this, 'arraySort')); (cause your method is private i assume THIS is the call you need)

If you want to call uasort from within an object instance, while the callback is a static method of the current class, then you should use uasort($arrayToSort,array('self','arraySort'));

Additionally you can simplify your sort method like this:

private function arraySort($a, $b) {  
    return strcmp($a, $b);
}

According to your answer, and assuming that the examples listed above are "strings", you need to split the values first, before sorting to get access to the actuall integer at the end of the string.

using uasort($array, array($this, 'arraySort')) in your class, along with

private function arraySort($a, $b) { 
    $intOfA = explode(":", $a);
    $intOfA = intval(trim($intOfA[1]));
    $intOfB = explode(":", $b);
    $intOfB = intval(trim($intOfB[1]));
    return $intOfA - $intOfB;
}

should do the trick.

Sign up to request clarification or add additional context in comments.

5 Comments

Psst, strcmp so you don't have to mess with data type issues.
@Mr.Llama that's right, but depends on what $a and $b are. (I assumed it to be the integers after the String)
my sort function is an instance method of the same object. Though with changing 'self' to $this I still get back an unsorted array. And yes it is to be sorted by the integers after the string
Sorry I was unclear in my response. The data I put in my question is an array, with the key and value seperated by a :. So while the GUID shown is a string, it is the key while the integer after the colon is an integer for it to be sorted by.
@dognose It looks like your initial response did work correctly. My web inspector was just not displaying the array in the same order it was given.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.