File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

MultiDimenaional Array sort

 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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!

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Here is the entire javascript,



The output.sort(sortByDensity) seems not to be working! Any clue?
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Eric,

Am I doing anything wrong here?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
So now you need to change your output code to loop through the array and read the objects.

Eric
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
But why would it not sort?
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
I'm just trying to look for a solution here. Please help me Eric!
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator


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
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
I did not quite get you. Can you be clear?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
You need to do the sort the way you originally did it.

Eric
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
    Bookmark Topic Watch Topic
  • New Topic