Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MultiDimenaional Array sort

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Here is the entire javascript,



The output.sort(sortByDensity) seems not to be working! Any clue?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Eric,

Am I doing anything wrong here?
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
So now you need to change your output code to loop through the array and read the objects.

Eric
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
But why would it not sort?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I'm just trying to look for a solution here. Please help me Eric!
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I did not quite get you. Can you be clear?
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You need to do the sort the way you originally did it.

Eric
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 https://coderanch.com/t/460904/HTML-JavaScript/Getting-top-keywords-from-text#2057070.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
    Bookmark Topic Watch Topic
  • New Topic