Solved

Sort JavaScript output

Posted on 2013-01-26
6
318 Views
Last Modified: 2013-01-27
This code works great, thanks to Slick812. But I need the output sorted by the number of times the duplicate string is found, in descending order.

Also prefer to list just the first 10 matches but that is not as important as the sort.
<!DOCTYPE html>
<html>
<head>
  
  <style type='text/css'>
		
	}
  body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}
body {
	background-color: #FFFFFF;
}
</style>

</head>
<body>




<script>
var tx1 = "Bobob is attempting-to find him the reason, Janet is attempting. to understand Bobob and to! reason with him a!s Bobob in reason or reason.";

function getDups(txt) { // Only has duplicates
// I had to remove most common punctuation
// BUT not every one, so BeWare if the text has other punctuation
txt=txt.replace(/,/g,"");
txt=txt.replace(/\./g,"");
txt=txt.replace(/\?/g,"");
txt=txt.replace(/!/g,"");
txt=txt.replace(/\"/g,"");
txt=txt.replace(/\'/g,"");
txt=txt.replace(/\-/g," ");
txt=txt.replace(/;/g,"");
txt=txt.replace(/:/g," ");

wordary = txt.split(' ');
wordary = wordary.sort();

var dNum = 1;
var dups = {};
for (var i=0; i < wordary.length-2; i++) {
if ( wordary[i].length > 4 
    && wordary[i+1].length > 4 
    && wordary[i+1] == wordary[i]) {
	++dNum;
	dups[wordary[i]] = dNum;
	} else dNum = 1;
}

txt = "";
var key = "";
for (key in dups) {
    txt+= key +" "+dups[key]+"<br />";
    }
return txt;
}


function showUm(text) {
text = getDups(text);
document.getElementById("de").innerHTML = text;
}

</script> 


<button onClick="showUm(tx1)"> Get Duplicates </button>
<p id="de"> </p> 

  
</body>
</html> 

Open in new window

0
Comment
Question by:Qsorb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 150 total points
ID: 38823429
test page : http://jsfiddle.net/LNDsY/2/
var tx1 = "Bobob is attempting-to find him the reason, Janet is attempting. to understand Bobob and to! reason with him a!s Bobob in reason or reason.";

function getDups(txt) { // Only has duplicates
    // I had to remove most common punctuation
    // BUT not every one, so BeWare if the text has other punctuation
    txt=txt.replace(/,/g,"");
    txt=txt.replace(/\./g,"");
    txt=txt.replace(/\?/g,"");
    txt=txt.replace(/!/g,"");
    txt=txt.replace(/\"/g,"");
    txt=txt.replace(/\'/g,"");
    txt=txt.replace(/\-/g," ");
    txt=txt.replace(/;/g,"");
    txt=txt.replace(/:/g," ");
    
    wordary = txt.split(' ');
    wordary = wordary.sort();
    
    var dNum = 1;
    var dups = {};
    for (var i=0; i < wordary.length-2; i++) {
    	if ( wordary[i].length > 4 
            && wordary[i+1].length > 4 
            && wordary[i+1] == wordary[i]) {
            ++dNum;
            dups[wordary[i]] = dNum;
        } else dNum = 1;
    }

	// CONVERT "HASH TABLE" TO ARRAY BECAUSE WE CAN'T SORT "HASH TABLE"
    var arr = [];
    for(var k in dups) {
        var obj = {}; // silly notation
        obj[k] = dups[k];
        arr.push(obj);
    }
  
    // SORT ARRAY
    arr.sort(function(d1,d2) {
        for(key1 in d1) break;
        for(key2 in d2) break;
        return d2[key2]-d1[key1];
    })
    
    txt = "";
    for(var i=0;i<arr.length;i++) {
        for(var k in arr[i]) break;
        txt+= k +" "+arr[i][k]+"<br />";
    }
    return txt;
}


function showUm(text) {
    text = getDups(text);
    document.getElementById("de").innerHTML = text;
}

Open in new window

0
 
LVL 34

Accepted Solution

by:
Slick812 earned 350 total points
ID: 38823495
here a function that does what you ask, but did not test it much, so may get bogged down with lengthy text inputs?

<script>
var tx1 = "Bobob attempting-to finds the finds (reason), finds att;empting. finds yoodol yoodol understand Bobob finds! reason finds Bobob? finds reason reason. ";
tx1 += "lengt hight money split lengt lower lengt hight hight:split other! hight hight split lower lower money. other yoodol uuuuu uuuuu"


function getDups(txt) {
txt=txt.replace(/[)(;,!\.\?]/g,"");
txt=txt.replace(/[:\-]/g," ");

wordary = txt.split(' ');
wordary = wordary.sort();

var dNum = 1, dups = {};
for (var i=0; i < wordary.length-2; i++) {
if ( wordary[i].length > 4 
    && wordary[i+1].length > 4 
    && wordary[i+1] == wordary[i]) {
	++dNum;
	dups[wordary[i]] = dNum;
	} else dNum = 1;
}
var key = "", pos = [];
wordary = [];
for (key in dups) {
	pos.push(key);
	wordary.push(dups[key]);
    }

var b = 0, a = wordary.length-1;;
while (b < a) {
for (i = b; i < a; i++) {
if (wordary[i] < wordary[a]) {dNum =wordary[i]; key = pos[i];
    wordary[i] = wordary[a]; pos[i] = pos[a];
    wordary[a] = dNum; pos[a] = key;
    }
}
--a;
}

txt = "";
dNum = wordary.length;
if (dNum > 10) dNum = 10;
for (i=0; i < dNum; i++) {

// CHANGE THIS HTML BELOW TO YOUR OUTPUT STYLE or LINE BREAKS
txt+= wordary[i]+" duplicates for<br/>. . <b>"+pos[i] +"</b><br />";
//txt+= "<b>"+pos[i] +"</b><br />. . has "+wordary[i]+" duplicates,<br />";
}
return txt;
}

function showUm(text) {
text = getDups(text);
document.getElementById("de").innerHTML = text;
}
</script>

Open in new window


Be sure to change the HTML to your specs on this line -

// CHANGE THIS HTML BELOW TO YOUR OUTPUT STYLE or LINE BREAKS
txt+= wordary+" duplicates for<br/>. . <b>"+pos +"</b><br />";

where wordary has the number and  pos  has the WORD
0
 

Author Closing Comment

by:Qsorb
ID: 38823537
leakim971 was first so he get's half the points because Slick812 gave me both, he also set the amount of the results shown to ten, so I'm awarding points equally.

Thanks guys for the quick and excellent work and no confusion.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:leakim971
ID: 38823942
150
350

Equally?
0
 

Author Comment

by:Qsorb
ID: 38825437
Sorry, that should have been equally. Not sure what happened.  How do I ask the moderator or whomever controls the points to divvy them equally?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38825443
no, of course :))
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 43
Jquery driving me nuts... 14 29
How to pass values to HTML-5 attribute dynamically? 24 53
Keyup listener getting fired on initial page load 1 19
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question