Home > Software engineering >  Move line of text to the top/bottom based on specific keyword
Move line of text to the top/bottom based on specific keyword

Time:09-05

I have a text box with many different lines of text in it. I would like to move any lines that contains specific case-sensitive keywords to either the top or the bottom of the entire text box. How can I do that?

Example Input:

Bob likes GRAPES but not oranges.

John likes APPLES but not pears.

Patrick likes ORANGES but not apples.

Joe likes PEARS but not oranges.

Desired algorithm: If line contains "APPLES" or "ORANGES", move to top or bottom of text.

Example Output:

Bob likes GRAPES but not oranges.

Joe likes PEARS but not oranges.

John likes APPLES but not pears.

Patrick likes ORANGES but not apples.

CodePudding user response:

const textarea = document.querySelector('textarea');

const AllLines = textarea.value.split("\n").filter(line => line!= '');

const linesWithApplesOranges = AllLines.filter(line => line.includes('APPLES') || line.includes('ORANGES'));

const linesWithoutApplesOranges = AllLines.filter(line => !line.includes('APPLES') && !line.includes('ORANGES'));

const newValue = [...linesWithoutApplesOranges, ...linesWithApplesOranges];

textarea.value = newValue.join("\n");
<textarea>
Bob likes GRAPES but not oranges.

John likes APPLES but not pears.

Patrick likes ORANGES but not apples.

Joe likes PEARS but not oranges.
</textarea>

CodePudding user response:

This should work, if I understood your conditions right.

let lines = [
    'Bob likes GRAPES but not oranges.',
    'John likes APPLES but not pears.',
    'Patrick likes ORANGES but not apples.',
    'Joe likes PEARS but not oranges.'
];
let output = [];
let top_or_bottom = [];

for (let i in lines) {
    let line = lines[i];
    // check if we have any of the keywords
    let applesIndex = line.indexOf('APPLES');
    let orangesIndex = line.indexOf('ORANGES');
    // if we have, save it for later
    if (applesIndex > -1) {
        top_or_bottom.push(line);
    } else if (orangesIndex > -1) {
        top_or_bottom.push(line);
    } else {
        // otherwise push it into regular array
        output.push(line);
    }
}
// if moving to the bottom
output = output.concat(top_or_bottom);
// if moving to the top
// output = top_or_bottom.concat(output);

CodePudding user response:

If you need, I can explain the solution

function modifyText() {
  const triggerWords = ['apple', 'orange']
  const textBox = document.getElementById('sampletext');
  const lines = textBox.value.split('\n').filter(a => a.trim().length > 0);
  const isTrigger = (s) => {
    return !!triggerWords.find(x => s.includes(x));
  };
  
  lines.sort((a, b) => {
    const aSmall = a.toLowerCase();
    const bSmall = b.toLowerCase();
    return isTrigger(aSmall) ?
      isTrigger(bSmall) ? 0 : 1 :
      isTrigger(bSmall) ? -1 : 0;
  });
  textBox.value = lines.join('\n');
}
<textarea id="sampletext" rows="12" cols="80">Some apples
some oranges
some things
some other stuff
Maybe apple maybe watermelon
</textarea>
<br/>
<button onClick="modifyText()">Click Me!</button>

  • Related