Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:12:17+00:00 2026-06-12T01:12:17+00:00

I am attempting to sort a list of nodes alphabetically, before echoing them out

  • 0

I am attempting to sort a list of nodes alphabetically, before echoing them out in SimpleXML. I am using a usort function similar to the one referenced here:

Sorting Results returned by SimpleXML, and Xpath in PHP

Here is my code:

$xQuery = $xml->comiclist->comic;

function cmp ($a, $b) {
     return strcmp(
         $a->mainsection->series->sortname, 
         $b->mainsection->series->sortname
     ); 
}

usort($xQuery, "cmp");

foreach ($xQuery as $comic) :

The issue is that it only seems to be partially working. Most of the items are grouped together correctly but, some are completely out of place. As you can see from the function, each $comic node is being sorted by its child “mainsection->series->sortname”.

Here’s an example of some of the order it’s generating:

<sortname>New Avengers: Illuminati, Vol. 2</sortname>
<sortname>New Avengers: Illuminati, Vol. 2</sortname>
<sortname>Nova</sortname>
<sortname>New X-Men</sortname>
<sortname>Nation X</sortname>
<sortname>Namor, The Sub-Mariner Annual</sortname>

As you can see, they are not in alphabetical order. Am I missing something? Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:12:18+00:00Added an answer on June 12, 2026 at 1:12 am

    From quickly looking at your question, it looks like that you’re using usort on an object of type SimpleXMLElement. However the function is intended to be used for an array.

    Probably converting the object (or more precisely the iterator it offers) to an array first should solve your issue.

    Explanation: In the example you’ve linked, the xpath function is used which already returns an array, here in your example that is not the case.

    A function to turn the offered iterator to an array is iterator_to_array:

    $array = iterator_to_array($xQuery, false);
    
    usort($array, "cmp");
    
    foreach ($array as $comic) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to do a simple bubble sort code to get familiar with list/string
I'm attempting to sort a ListView using C#, but whenever I click the sort
So I am pulling a list of links and I am attempting to sort
I am attempting to sort a Python list of int s and then use
I am attempting to implement heap sort using the psuedo code from the book
I'm attempting to sort a list of strings in a locale-aware manner. I've used
I am attempting to alphabetically organize an options list populated from an array; the
I'm attempting to use Enumerable.OrderBy to sort a List because ultimately I want to
I am attempting to sort a hashmap on type <Integer,Double> using a TreeMap and
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.