Sort array from smallest value to biggest?

Wick Editor 1.18

Describe the Problem

I want to make a new array that is sorted from the smallest number to the greatest.
Here’s an example,

// I have this array:
this.originalArray = ["356","628","871","189"];

//I want to have another array that's sorted from smallest to biggest value
this.sortedArray = ["189","356","628","871"];

// Is it possible to have a code that sorts it?

What have you tried so far?

I tried googling some methods.
I found some solutions online, but not what I needed.


Do you need the numbers to be in quotes as strings…? Because that would be different. It will sort based on the ascii.

I have the numbers in quotes, but I can unquote em’

So anyway is fine

(If I multiply a quoted number by 1, the returned value would be unquoted)

Did you tried this? (from w3schools)

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
2 Likes

Thank you Jovanny this worked :D

w3schools is a great site

1 Like

What did you search up? When I search up “js sort array” I get Jovanny’s solution as the first result?

If you want to search up how to do something, I recommend you put your programming language in the search query (as what you are looking for may be a built-in feature, and you get the benefit of being able to simply copy+paste code you find if it’s something like an algorithm) and try to simplify the search query. Did you put “wick” in the query? If so, that might not give you good results (since Wick isn’t that well-known). But Wick uses JavaScript, and that is very widely used, so if you put “javascript” or “js” you will get better results.

I think you can use +quote instead, I think it’s faster. You can also use parseFloat(quote) or parseInt(quote) instead but I’m not sure how efficient they are. Use parseFloat if what you want is a number with a decimal point, and parseInt if it has no decimal point.

P.S. parseFloat is called that way because numbers with decimal points in JavaScript are really what is called “floating-point numbers”, in case you didn’t know. I think it’s called that way because the decimal point can move around? Search that up

another edit: i moved where “in JavaScript” was in case people might get confused because floating-point numbers are used everywhere and they are called that everywhere. and the way i wrote it made it so it can be interpreted as “in JavaScript terminology, they are called floating-point numbers” and not “JavaScript uses floating-point numbers”

2 Likes

I searched “sort array from smallest to largest value java,” the first result was stacks overflow. I’m actually surprised w3schools wasn’t with any of the results, I probably should’ve tried searching something else.

Thanks, I’ll try these rn. It does sound interesting

Oh, i see why you got unsatisfactory results. I think you think Java and JavaScript are similiar languages, but even though they have similar names I’m pretty sure they’re not. For example, Java is strongly typed, so when you want to make a variable, you must say what type it is (like if it’s a number, string, Boolean, e.t.c.), and the variable cannot change type. But you can’t say what type a variable is in JavaScript because it doesn’t matter, so it is weakly typed. And there is also dynamic memory allocation in Java which doesn’t really exist in JavaScript. I have never actually written a program in Java so I don’t actually really know anything about it (and i only know there’s dynamic memory allocation because i searched up “how to make dynamic array” for a c++ program and i found a page with java code i continued with it because java and c++ have similar syntaxes so converting the code to c++ wouldn’t be hard and found out java also has the new operator which works the same way c++'s does).

Umm that was long tl;dr don’t confuse java with javascript because they are different languages

Or maybe you don’t know Java exists and you just wrote “java” as a shorthand for “javascript” ignorant of the fact there is another programming language called that.

2 Likes

Wow, you just blew my mind by saying “Java and JavaScript are two different things”

I always thought Java was short for JavaScript

Yes. I should really start researching coding languages rather than researching how to use em’

1 Like

also i searched up online and javascript was named that because netscape (an old web browser) supported java applets, and also probably because java was popular (and something about a licensing deal sun microsystems who created java)

2 Likes