how to sort a string in java This is a topic that many people are looking for. cfiva.org is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, cfiva.org would like to introduce to you How to sort a string in java (without using String method). Following along are instructions in the video below:
Otherwise welcome to take me devoted in this video. I will show how to short short a stream. Suppose our string is a r st.
And said. Then the our should be a r st. That actually ive written in sorted order.
Only suppose our listing. It said as a b anything so. What should be our mm output.
A then b then s. Then z in this manner in extending order. So what do we do that so we first take input from user okay you know now what will we do we will convert this string to character array.
Okay so we will take one can array take any ch ar is equal to str start to carry a okay now now we will write for loop in zero. Yes. I less than ch chart.
Sorry. You cant see it jana drugs. Glint.
Okay. No hi fine. Well take again one look.
What we have we are going to do we are going to compare its index wise and if it is smaller. Then what we will do will swipe to so that the lower character value means. The lower value of that character will be come to the left side and higher will be the right side.
So that it will be done in x. And in water okay. So again.
We write for loop. Then in into j is equal to 0. Then j is then you can see it dot length then j.
Plus for this. Its actually very simple just we need to write the step function here if see h. A character array of i is less than ch character edge of g.
Then what we are going to do this is swept. What was that we will use temporary variable time and we will put ch of character array oh hi. We have assigned character of i into.
10. Okay. You know we have to write somewhere to stay we havent in its like defined here so lets define anywhere here we will define care tamed okay no i want to sorry sorry sorry.
Because i have to today yes okay no not just see its a character. We will assign character a value we will assign the value of character of j. Into character head of j.
2. Pi. So again.
I have written okay. Okay. Done no.
We will assign temporarily to character array of j. Thats it now lets format. It okay now what will we do there is we will again we have to write or loop.
See here we are just swapping. So it will be the character with value will be in sorted order. Now we have to convert indoors character sorted order character into string.
So how we will do like that in ted take. 0. Then in case less than what we have taken ch a hunter.
What length then a thats plus okay. What will we do here. We will we have to define one term one variable that contains sorted string.
So let us define here string. Lets say sorted string. Okay.
No lets in your slides with empty string. Okay now what would we do they will i started a string is equal to sorted. String plus.
Plus. What plus this one character array of yes. Okay okay now for each for we have defined a low.
Then if k is equal to zero means for 0th index then it will be store in this string. Okay first it is empty then in reverse order. Suppose if you will take that as a b.
Then first a will come then b then s. Then z. Now this is done lets print a lets split.
Which this one shorter string. So let us write something structured string. Is done so this is our code.
It is very simple just we need to write this wave function okay and before that we have to convert this into character array. Then we have to process this array and then we have to just write our string. Okay so if you run this okay first look.
Lets take example of this one only z is a b c a b. I said okay now one more time and lets write it take me do so. What is the salted string for that c cd eee hm whatever so it is basically for c.
Then d. It is in in extending order is there okay so guys. This is all about auto salt thirsty.
Although we can use a string api method using compared to because a string is yeah it implements comparable interface. So we can do that but this is basic logic and we should know this and when we will when we will go through strings. Then string class and all you know you might have known you must know this and then we can see that thing also auto reverse a string or to sort a string.
But this is basic and you should know this logic okay thanks for watching dont forget to subscribe to get updated with latest videos. Thank you music .
Thank you for watching all the articles on the topic How to sort a string in java (without using String method). All shares of cfiva.org are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.