[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sort JavaScript output

Posted on 2013-01-26
6
Medium Priority
?
323 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 600 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 1400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

656 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