Alphabetical Sorting of String
- To sort a string, we first need to convert it into a character array
- The
Arrays.sort()
method is avoid
method and does not return a value. it sorts the array in place. - Finally, we need to convert the sorted character array back into a string
- We can sort a string using
sorted()
without converting it to a character array. - However, we still need to use
''.join()
to obtain a sorted string becausesorted()
returns a list
- C++
std::sort()
works directly on astd::string
without needing to convert it to a character array, because astd::string
in C++ is essentially a sequence of characters (similar to a char array) - Just like in Java,
std::sort()
returnsvoid
Defining Custom Comparison Logic
- The comparator logic above implements a descending order. To achieve ascending order, swap
other.freq
andthis.freq
- For more details, refer to Java Comparison
- The comparator logic above implements a descending order. To achieve ascending order, swap
other.freq
andself.freq
- The comparator logic above implements a descending order. To achieve ascending order, swap
other.freq
andself->freq
Heap Manipulation
Convert HTML to TXT
Important
We don’t want to extract the content as a single string because this would result in losing all the formatting information provided by the HTML tags. Reformatting the content based on the document structure afterward is tedious, error-prone, and not scalable.
Instead, the idea is to retain the formatting information we need before removing all the HTML tags. Then, we can use this retained formatting information to generate a text file with the desired formatting.
Code
This above code example assumes that
<br />
is the only tag used to denote line breaks in the given HTML string. If other tags or methods are used for formatting, additional handling may be required.Also note that, The
FileWriter writer
formats and writes the content into a text file, ensuring that line breaks are correctly represented using\n
.