0

I have a little problem to solve. I want to compare two texts if they are the same.

let currentValue = "24960307.W 25880305.W 24880208.W 25650156.W"

let newValue = "24880208.W 24960307.W 25650156.W 25880305.W"

// is the same text just diferent order

// when i did includes 
let x = currentValue.includes(value);
console.log(x);


//response in console
false

I tried with includes and localeCompare but still show that text is different.

5
  • 3
    They are different. How exactly are they supposed to be compared? What’s the rule? Commented Nov 14, 2022 at 14:56
  • What's value in that code? I mean... you have to check each part of the two strings if order is not important and you're stuck with string representation. This isn't the same text, but may be the same value for your purposes. Commented Nov 14, 2022 at 15:00
  • With "is the same text just diferent [sic] order" are you referring to the different parts of the strings? I think you want to split the strings on, say, a space, and compare the arrays. Commented Nov 14, 2022 at 15:01
  • @SebastianSimon rule is just i want to compare this 2 tests value must be the same doesnt mether if "word" come first second or last". Commented Nov 14, 2022 at 15:23
  • @HereticMonkey sorry i just start to learn JS i dont know to specific(professional) to explain. i just can say what i wan to do. I got anwser form #Tim Biegeleisen and work just i want. Commented Nov 14, 2022 at 15:26

1 Answer 1

0

A quick solution to normalize order is to split, sort, rejoin, then compare. This avoids any fancy parsing.

let currentValue = "24960307.W 25880305.W 24880208.W 25650156.W"
let newValue = "25880305.W 24880208.W 25650156.W 24960307.W"


const a = currentValue.split(' ').sort().join(' ')
const b = newValue.split(' ').sort().join(' ')

console.log(a === b)

let x = a.localeCompare(b) === 0
console.log(x);

Sign up to request clarification or add additional context in comments.

1 Comment

than you this works to how i want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.