Solved

Sort JavaScript output

Posted on 2013-01-26
6
312 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
  • 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 33

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now