Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sort Function Issue

  1. #1
    Junior Member
    Join Date
    Mar 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sort Function Issue

    Hey guys so im a highschool student and I just started to get into coding. I have this project that we are doing in class were I have to design a page then write a sort function to sort a list of cars prices from least to greatest and greatest to least. I have managed to get the sorting correct with the numbers but I can't figure out how to relate the prices with the title of the car that I am sorting so that I can move the title with the price. Does anyone know how I can accomplish this?

    Here's my code:


    onEvent("sortDropDown", "change", function( ) {
    var sortDropDown = getText("sortDropDown");
    var prices = [(getNumber("carPrice0")), (getNumber("carPrice1")), (getNumber("carPrice2"))];
    var i = 0;
    if (sortDropDown === "Price: Greatest to Least") {
    descendingOrder(prices);
    for (i = 0; i < 3; i++) {
    setProperty("carPrice" + [i], "text", prices[i]);
    }
    } else if ((sortDropDown === "Price: Least to Greatest")) {
    numericalOrder(prices);
    for (i = 0; i < 3; i++) {
    setProperty("carPrice" + [i], "text", prices[i]);
    }
    } else {
    setCar(currentCarType);
    }
    });


    function numericalOrder(list) {
    var holder = 0;
    for (var i = 0; i < list.length; i++) {
    for (var j = i; j < list.length; j++) {
    if (list[j] < list[i]) {
    holder = list[j];
    list[j] = list[i];
    list[i] = holder;
    }
    }
    }
    return list;
    }

    function descendingOrder(list) {
    var holder = 0;
    for (var i = 0; i < list.length; i++) {
    for (var j = i; j < list.length; j++) {
    if (list[j] > list[i]) {
    holder = list[j];
    list[j] = list[i];
    list[i] = holder;
    }
    }
    }
    return list;
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Sort Function Issue

    This code doesn't look like java. Moved to other languages section.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Well Ordered Sort function
    By GStateSwish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 7th, 2013, 11:58 PM
  2. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  3. Buble sort issue
    By kobi1988 in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 7th, 2011, 09:43 AM
  4. I Cant seem to get my sort routine to function
    By rrickman9 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 22nd, 2011, 07:48 PM