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 953645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:05:04+00:00 2026-05-16T00:05:04+00:00

I am loading some XML data, building it into an unordered list and displaying

  • 0

I am loading some XML data, building it into an unordered list and displaying it. I now need to sort this data so it is displayed in a random order each time it is loaded. Do I need to completely change how I am doing this below or is there an easy way to get the data and sort it before using my existing code to format it for output?

Here is the straight load and display code:

var out = '<li>';
$(xml).find('person').each(function(){
    counter++;

    var person_image   = $(this).find('person_image').text();
    var person_name    = $(this).find('person_name').text();
    var person_title   = $(this).find('person_title').text();
    var person_company = $(this).find('person_company').text();

    out = out + '<div><a href="foo/'+ belongsTo +'.php#'+ person_image +'"><img src=/images/' + belongsTo + '/' + person_image + '.jpg />' + '<br /><label><span>' + person_name + '</span><br />' + person_title + '<br />' + person_company + '</label></a><br /></div>';

    if(counter % 3 === 0 && counter !== total){
        out = out + '</li><li>';
    }
});
out = out + '</li>';
$('#display_block').html(out);

As you can see I am building the list items as I load them, in the order they are retrieved from the XML… this is what I need to randomly sort. I suppose I need to get everything first (into an array? Associative array? Object? Not sure how to best do this), randomly sort it and then iterate through building my out variable?

How would I do what I’m doing above but stick some sort of random sorting in the process?

As for randomly sorting, I came up with the following which seems to work:

function randomsort(a, b){ a=Math.random(); b = Math.random(); return a - b }

Just need to know how to gather the data so i can apply this sorting. Thanks!

  • 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-05-16T00:05:05+00:00Added an answer on May 16, 2026 at 12:05 am

    You could create a seperate array of the same length as the number of “person” elements in your XML. Fill this array with the keys to the XML elements (0 to n) and randomly sort it using your function. Then simply keep shifting items off this array and use the values you get to access the collection of XML elements.

    That way, you won’t have to manipulate the XML data (destroying the original structure) and keep most of the code you already have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary of the issue: When I am loading some data into my iPhone application
In my application, I'm loading some data from a file. This can fail and
I am trying to load data from XML string into a structure of some
I am loading a huge amount of data from an XML file. This data
I have to load some XML data (generated from a database, using PHP) into
I am loading some template files into my application using require.js like so: define(function(require)
I'm loading some HTML via Ajax with this format: <div id=div1> ... some content
I'm loading some feeds and displaying them as links on my site page. When
I am loading some text dynamically into a div, but have the problem that
Let's say I do this: $(#content).load(...); Within what I'm loading some javascript is included:

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.