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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:15:44+00:00 2026-06-11T02:15:44+00:00

I’m trying to write a script that chooses three items at random from a

  • 0

I’m trying to write a script that chooses three items at random from a list of objects, alphabetizes them by one field, and then displays the alphabetized values of another field. So far I can get it to choose the items randomly and show them, but it’s still not sorting. Here’s my code:

function random_text() {};
var random_text = new random_text();
var number = 0;
random_text[number++] = {
    show: "Bob",
    sort: "A"
}
random_text[number++] = {
    show: "Casey",
    sort: "B"
}
random_text[number++] = {
    show: "Dan",
    sort: "C"
}
random_text[number++] = {
    show: "Alfred",
    sort: "D"
}

var random_number = Math.floor(Math.random() * number);
var random_number1 = Math.floor(Math.random() * number);
var random_number2 = Math.floor(Math.random() * number);

if (random_number !== random_number1) {
    var name1 = random_text[random_number1].show;
} else {
    var name1 = "";
}

if (random_number !== random_number2 && random_number1 !== random_number2) {
    var name2 = random_text[random_number2].show;
} else {
    var name2 = "";
}

var name3 = random_text[random_number].show;

var objs = [name1 + " " + name2 + " " + name3];

objs.sort(function (a, b) {
    var nameA = a.sort.toLowerCase(),
        nameB = b.sort.toLowerCase()
        if (nameA < nameB) //sort string ascending
    return -1
    if (nameA > nameB) return 1
    return 0 //default return value (no sorting)
})

document.write(objs.sort());

Thanks in advance for the help. If there’s any way I can clean up the code, I appreciate tips on that, too.

  • 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-11T02:15:45+00:00Added an answer on June 11, 2026 at 2:15 am

    See http://jsfiddle.net/Nc2Je/

    var random_text = [
        {
            show: "Bob",
            sort: "A"
        },
        {
            show: "Casey",
            sort: "B"
        },
        {
            show: "Dan",
            sort: "C"
        },
        {
            show: "Alfred",
            sort: "D"
        }
    ],random_numbers = new Array(),
        objs=new Array();
    for(var i=0;i<3;i++){
        function random(){
            var n=Math.floor(Math.random() * random_text.length);
            if(random_numbers.indexOf(n)===-1){return n;}
            return random();
        }
        random_numbers.push(random());
        objs.push(random_text[random_numbers[i]])
    }
    var alphabetized=new Array();
    outerloop:
    for(var i=0;i<objs.length;i++){
        for(var j=0;j<alphabetized.length;j++){
            if(alphabetized[j].sort>objs[i].sort){
                alphabetized.splice(j,0,objs[i]);
                continue outerloop;
            }
        }
        alphabetized.push(objs[i]);
    }
    alphabetized.getNames=function () {
        var arr=new Array();
        for(var i=0;i<this.length;i++){
            arr.push(this[i].show);
        }
        return arr;
    }
    
    document.write(alphabetized.getNames());
    

    Edit:

    You do some weird things:

    1- Why do you create an empty function and then you overwrite it with an instance of itself?

    function random_text() {};
    var random_text = new random_text();
    

    2- If you want an object which works like an array, why don’t you use arrays instead of setting array-like properties to random_text?

    var number = 0;
    random_text[number++] = {
        show: "Bob",
        sort: "A"
    }
    random_text[number++] = {
        show: "Casey",
        sort: "B"
    }
    random_text[number++] = {
        show: "Dan",
        sort: "C"
    }
    random_text[number++] = {
        show: "Alfred",
        sort: "D"
    }
    

    3- You should use the array random_numbers instead of those variables:

    var random_number = Math.floor(Math.random() * number);
    var random_number1 = Math.floor(Math.random() * number);
    var random_number2 = Math.floor(Math.random() * number);
    

    4- Instead of those weird checks, you should ensure that your random number is different than the previous random numbers. With your checks, maybe the three random numbers are the same, so you will have only one name.

    if (random_number !== random_number1) {
        var name1 = random_text[random_number1].show;
    } else {
        var name1 = "";
    }
    
    if (random_number !== random_number2 && random_number1 !== random_number2) {
        var name2 = random_text[random_number2].show;
    } else {
        var name2 = "";
    }
    
    var name3 = random_text[random_number].show;
    

    5- Why do you create an array which has only one element (a string)?

    var objs = [name1 + " " + name2 + " " + name3];
    

    6- I didn’t use your following code because I have never used that, so I wrote my own sorting function.

    objs.sort(function (a, b) {
        var nameA = a.sort.toLowerCase(),
            nameB = b.sort.toLowerCase()
            if (nameA < nameB) //sort string ascending
        return -1
        if (nameA > nameB) return 1
        return 0 //default return value (no sorting)
    })
    

    7- Once you have sorted your objects, if you want to write them you should use document.write(objs) without sort().

    Edit 2:

    alphabetized.getNames returns an array.

    This way you can do

    var names=alphabetized.getNames();
    names.length;
    names[0];
    ...
    

    And if you want an string, you can do alphabetized.getNames().join(" ");

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.