Solved

Sort JavaScript output

Posted on 2013-01-26
6
319 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

717 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