File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes MultiDimenaional Array sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply locked New topic
Author

MultiDimenaional Array sort

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

Guys,

How could I sort a multidimensional array in a javascript. I tried the following but it gives me undefined as the value when I try to sort it.



Help me out guys!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
have a script on my blog, see if it helps:

http://radio.javaranch.com/pascarello/2009/05/29/1243615124260.html

Eric
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

That seems to give me some other results...it even includes the entire function as an additional element.

But what is wrong in the script that I provided above?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I am guessing you have a problem with your array. How are you doing the looping and adding to it?

Eric

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

Here is the entire javascript,



The output.sort(sortByDensity) seems not to be working! Any clue?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

I'm trying to do the following,

(1) In the text which is entered in the textarea, I would filter out the stop words
(2) Find out the keyword density
(3) Take the first 3 words and send them to the server using a form submit

I'm stuck with the above example. Please help!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

Eric,

Am I doing anything wrong here?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
This is a string:



You can not treat that as an object like you do

It should be



Now you have an object

Eric
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

But that did not help either....Got the below output,

Keyword Density
Total Words:25
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
So now you need to change your output code to loop through the array and read the objects.

Eric
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

But why would it not sort?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

I'm just trying to look for a solution here. Please help me Eric!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I am trying to help you learn.

You created a String



In Java, would you be able to reference things out of a string like a hash? no, you can not do that in JavaScript either. So you need an object in each index so you can use dot notation.

Since you have objects when you convert it to a string [via join] it says Object. So you need to loop through your array, index by index and read your properties of each individual object.

Eric
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

I infact did loop through it and now started to get some other errors which looks totally irrelevant. Below is the entire stuff that I did,




When I tried running it, it gave me Object Expected at line 126, which looks totally irrevevant!

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377



This is the function that you need to have a look at, I don't think you are dealing with the object correctly to output it.
Firstly, I don't think it is a 2D array, more like a List<Map> in java terms.

Sean


I love this place!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You do not have a multiple dimensional array, a multiple dimensional array would look like

[[1,2],[2,3],[4,5]]

so my first link would not work.

You should have left

# //var x = a.Density;
# //var y = b.Density;
# //return ((x < y) ? -1 : ((x > y) ? 1 : 0));

alone.

Eric
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

I did not quite get you. Can you be clear?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You need to do the sort the way you originally did it.

Eric
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

Man this is really getting me irritated. I have made some refactoring to the HTML and have started a new topic here just to avoid confusions.

I gave up sorting and now just getting the top three elements and will be continued in this post http://www.coderanch.com/t/460904/HTML-JavaScript/Getting-top-keywords-from-text#2057070.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9381
    
    2

Eric,

I gave up sorting. Can you please help me out with the other post from the link that I gave above? Any yes, please close this post!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MultiDimenaional Array sort