Solved

Sort JavaScript output

Posted on 2013-01-26
6
315 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

785 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